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[0−9]+)/
’, views.detail, name=’detail’),
url(r’^(?P[0-9]+)/results/
′,views.results,name=′results′),url(r′(?P[0−9]+)/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 reversefrom .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 datetimefrom django.utils import timezone
from django.test import TestCasefrom .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)