美文网首页
04 - Views and templates

04 - Views and templates

作者: AureliusShu | 来源:发表于2018-09-12 21:19 被阅读0次

Views and templates

  1. Writing more views

    # polls/views.py 
    # views response
    def detail(request, question_id):
        return HttpResponse("You're looking at question %s." % question_id)
    
    def results(request, question_id):
        response = "You're looking at the results of question %s."
        return HttpResponse(response % question_id)
    
    def vote(request, question_id):
        return HttpResponse("You're voting on question %s." % question_id)
    
    # polls/urls.py
    # adding the following path calls
    from django.urls import path
    from . import views
    
    urlpatterns = [
        # ex: /polls/
        path('', views.index, name='index'),
        # ex: /polls/5/
        path('<int:question_id>/', views.detail, name='detail'),
        # ex: /polls/5/results/
        path('<int:question_id>/results/', views.results, name='results'),
        # ex: /polls/5/vote/
        path('<int:question_id>/vote/', views.vote, name='vote'),
    ]
    
  2. Write views that actually do something

    # polls/views.py
    from django.http import HttpResponse
    from .models import Question
    
    def index(request):
        latest_question_list = Question.objects.order_by('-pub_date')[:5]
        output = ', '.join([q.question_text for q in latest_question_list])
        return HttpResponse(output)
    
    # Leave the rest of the views (detail, results, vote) unchanged
    
    # polls/templates/polls/index.html
    # Put the following code in that template
    {% if latest_question_list %}
        <ul>
        {% for question in latest_question_list %}
            <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
        {% endfor %}
        </ul>
    {% else %}
        <p>No polls are available.</p>
    {% endif %}
    
    # polls/views.py
    # update our index view in polls/views.py to use the template
    from django.http import HttpResponse
    from django.template import loader
    from .models import Question
    
    def index(request):
        latest_question_list = Question.objects.order_by('-pub_date')[:5]
        template = loader.get_template('polls/index.html')
        context = {
            'latest_question_list': latest_question_list,
        }
        return HttpResponse(template.render(context, request))
    
  3. A Shortcut function: render()

    # polls/views.py
    from django.shortcuts import render
    from .models import Question
    
    def index(request):
        latest_question_list = Question.objects.order_by('-pub_date')[:5]
        context = {'latest_question_list': latest_question_list}
        return render(request, 'polls/index.html', context)
    
  4. A shortcut function: get_object_or_404()

    # polls/views.py
    # Raising a 404 error
    from django.http import Http404
    from django.shortcuts import render
    
    from .models import Question
    # ...
    def detail(request, question_id):
        try:
            question = Question.objects.get(pk=question_id)
        except Question.DoesNotExist:
            raise Http404("Question does not exist")
        return render(request, 'polls/detail.html', {'question': question})
    
    <!-- polls/templates/polls/detail.html -->
    {{ question }}
    
    # polls/views.py
    from django.shortcuts import get_object_or_404, render
    from .models import Question
    # ...
    def detail(request, question_id):
        question = get_object_or_404(Question, pk=question_id)
        return render(request, 'polls/detail.html', {'question': question})
    
  5. Use the template system

    <!-- polls/templates/polls/detail.html -->
    <h1>{{ question.question_text }}</h1>
    <ul>
    {% for choice in question.choice_set.all %}
        <li>{{ choice.choice_text }}</li>
    {% endfor %}
    </ul>
    
  6. Removing hardcoded URLs in templates

    <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
    <li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li></pre>    
    
    # the 'name' value as called by the {% url %} template tag
    path('<int:question_id>/', views.detail, name='detail'),
    # added the word 'specifics'
    path('specifics/<int:question_id>/', views.detail, name='detail'),
    
  7. Namespacing URL names

    # polls/urls.py
    from django.urls import path
    from . import views
    
    app_name = 'polls'
    urlpatterns = [
        path('', views.index, name='index'),
        path('<int:question_id>/', views.detail, name='detail'),
        path('<int:question_id>/results/', views.results, name='results'),
        path('<int:question_id>/vote/', views.vote, name='vote'),
    ]
    
    polls/templates/polls/index.html
    <li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>
    to point at the namespaced detail view:
    
    polls/templates/polls/index.html
    <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
    

相关文章

网友评论

      本文标题:04 - Views and templates

      本文链接:https://www.haomeiwen.com/subject/dfobgftx.html