代码之家  ›  专栏  ›  技术社区  ›  Sam

Django first应用程序

  •  0
  • Sam  · 技术社区  · 8 年前

    我的第一个django应用程序(它的名字是magghy)有一些问题。我正处于发展的开始阶段。因此,我有: magghy/URL。py:

    from django.conf.urls import *
    from magghy import views
    
    from . import views
    
    app_name = 'magghy'
    
    urlpatterns = [
        # esempio: /magghy/
        url(r'^$', views.index, name='index'),
        #esempio: /magghy/5/
        url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
        # ex: /magghy/5/results/
        url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
        # ex: /magghy/5/vote/
        url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
    ]
    

    在magghy/views中。py:

    from __future__ import unicode_literals
    
    from django.shortcuts import get_object_or_404, render
    from django.http import HttpResponseRedirect, HttpResponse
    from .models import Choice, Question
    from django.template import loader
    from django.urls import reverse
    
    
    
    #visualizzare domande e argomento specifico - collegare con modulo magghy.urls 
    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)
    
    #visualizzare pagina html secondo schema index.html oppure html 404 (eccezione)
    def index(request):
        latest_question_list = Question.objects.order_by('-pub_date')[:5]
        template = loader.get_template('magghy/index.html')
        context = RequestContext (request, {
            'latest_question_list': latest_question_list,
        })
        return HttpResponse(template.render(context))
    
    #visualizzare pagina 404    
    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})
    

    from django.conf.urls import *
    from django.contrib import admin
    from magghy import views
    
    urlpatterns = [
        url(r'^magghy/', views.detail),
        url(r'^admin/', include (admin.site.urls)),
    ]
    

    页面 http://127.0.0.1:8000/admin/ 工作完美! 页面 http://127.0.0.1:8000/magghy/ 不起作用 页面 http://127.0.0.1:8000/magghy/5 不起作用

    在这两种情况下,终端日志为:

    Internal Server Error: /magghy/
    Traceback (most recent call last):
      File "/Library/Python/2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
        response = get_response(request)
      File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
        response = self.process_exception_by_middleware(e, request)
      File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
        response = wrapped_callback(request, *callback_args, **callback_kwargs)
    TypeError: detail() takes exactly 2 arguments (1 given)
    

    拜托,你能帮我吗?谢谢!

    阿德里

    1 回复  |  直到 8 年前
        1
  •  0
  •   Daniel Roseman    8 年前

    mysite/URL。py直接链接到细节视图,而不是包括应用程序URL。它应该是:

    urlpatterns = [
        url(r'^magghy/', include('magghy.urls')),
        url(r'^admin/', include (admin.site.urls)),
    ]