Django

wampserver(Apache)+django
http://www.cnblogs.com/Junsept/p/6862595.html
1.时间:
from django.utils import timezone
timezon.now()

2.字符串表达式:
HttpResponse(“You’re looking at question %s.” % question_id)

3.返回值:
from django.template import loader

4.url
polls/url.py
app_name = ‘polls’
urlpatterns = [
url(r’^ ,views.index,name=index),url(r(?P[09]+)/ ’, views.detail, name=’detail’),
url(r’^(?P[0-9]+)/results/ ,views.results,name=results),url(r(?P[09]+)/vote/ ’, views.vote, name=’vote’),
]
polls/index.html

  • {{ question.question_text }}
  • from django.shortcuts import get_object_or_404, render
    from django.http import HttpResponseRedirect, HttpResponse
    from django.urls import reverse

    from .models import Choice, Question

    def vote(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    try:
    selected_choice = question.choice_set.get(pk=request.POST[‘choice’])
    except (KeyError, Choice.DoesNotExist):
    # Redisplay the question voting form.
    return render(request, ‘polls/detail.html’, {
    ‘question’: question,
    ‘error_message’: “You didn’t select a choice.”,
    })
    else:
    selected_choice.votes += 1
    selected_choice.save()
    # Always return an HttpResponseRedirect after successfully dealing
    # with POST data. This prevents data from being posted twice if a
    # user hits the Back button.
    return HttpResponseRedirect(reverse(‘polls:results’, args=(question.id,)))

    polls/tests.py
    import datetime

    from django.utils import timezone
    from django.test import TestCase

    from .models import Question

    class QuestionModelTests(TestCase):

    def test_was_published_recently_with_future_question(self):
        """
        was_published_recently() returns False for questions whose pub_date
        is in the future.
        """
        time = timezone.now() + datetime.timedelta(days=30)
        future_question = Question(pub_date=time)
        self.assertIs(future_question.was_published_recently(), False)
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值