【问题标题】:Django first appDjango 第一个应用程序
【发布时间】:2017-10-20 14:23:40
【问题描述】:

我的第一个 django 应用程序(它的名字叫 magghy)有一些问题。我正处于开发的开始阶段。所以我有: 马吉/urls.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})

在 mysite/urls.py 中:

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)

拜托,你能帮帮我吗?非常感谢!

阿德里

【问题讨论】:

    标签: python django


    【解决方案1】:

    mysite/urls.py 直接链接到详细视图,而不是包含应用程序 URL。应该是:

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

    【讨论】:

      猜你喜欢
      • 2020-12-09
      • 2018-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-16
      • 2017-07-02
      • 1970-01-01
      相关资源
      最近更新 更多