程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了为什么我在 Django 3.2大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决为什么我在 Django 3.2?

开发过程中遇到为什么我在 Django 3.2的问题如何解决?下面主要结合日常开发的经验,给出你关于为什么我在 Django 3.2的解决方法建议,希望对你解决为什么我在 Django 3.2有所启发或帮助;

我知道已经有人问过这个问题,但没有一个解决方案特定于我正在使用的 Django 版本,即 3.2。所以我正在关注 Django 教程,但我收到了这个错误。我在 Django 中使用通用视图,就像教程一样。在使用通用视图之前,一切正常。我是否必须对我的视图进行硬编码,或者我是否错误地实现了通用视图?

Internal Server Error: /polls/
Traceback (most recent call last):
  file "C:\Users\miche\tutorial-env\lib\site-packages\django\core\handlers\exception.py",line 47,in inner
    response = get_response(request)
  file "C:\Users\miche\tutorial-env\lib\site-packages\django\core\handlers\base.py",line 204,in _get_response
    response = response.render()
  file "C:\Users\miche\tutorial-env\lib\site-packages\django\template\response.py",line 105,in render
    self.content = self.rendered_content
  file "C:\Users\miche\tutorial-env\lib\site-packages\django\template\response.py",line 83,in rendered_content
    return template.render(context,self._request)
  file "C:\Users\miche\tutorial-env\lib\site-packages\django\template\backends\django.py",line 61,in render
    return self.template.render(context)
  file "C:\Users\miche\tutorial-env\lib\site-packages\django\template\base.py",line 170,in render
    return self._render(context)
  file "C:\Users\miche\tutorial-env\lib\site-packages\django\template\base.py",line 162,in _render
    return self.nodeList.render(context)
  file "C:\Users\miche\tutorial-env\lib\site-packages\django\template\base.py",line 938,in render
    bit = node.render_annotated(context)
  file "C:\Users\miche\tutorial-env\lib\site-packages\django\template\base.py",line 905,in render_annotated
    return self.render(context)
  file "C:\Users\miche\tutorial-env\lib\site-packages\django\template\defaultTags.py",line 312,in render
    return nodeList.render(context)
  file "C:\Users\miche\tutorial-env\lib\site-packages\django\template\base.py",line 211,in render
    nodeList.append(node.render_annotated(context))
  file "C:\Users\miche\tutorial-env\lib\site-packages\django\template\base.py",line 446,in render
    url = reverse(vIEw_name,args=args,kwargs=kwargs,current_app=current_app)
  file "C:\Users\miche\tutorial-env\lib\site-packages\django\urls\base.py",line 86,in reverse
    return resolver._reverse_with_prefix(vIEw,prefix,*args,**kwargs)
  file "C:\Users\miche\tutorial-env\lib\site-packages\django\urls\resolvers.py",line 694,in _reverse_with_prefix
    raise noreverseMatch(msg)
django.urls.exceptions.noreverseMatch: Reverse for 'detail' with arguments '(1,)' not found. 1 pattern(s) trIEd: ['polls/<int:pk/$']
[25/Apr/2021 17:05:11] "GET /polls/ http/1.1" 500 133372
Not Found: /polls/1/
[25/Apr/2021 17:33:23] "GET /polls/1/ http/1.1" 404 2715

这是我的代码: urls.py

from django.urls import path
from . import vIEws

app_name = "polls"
urlpatterns = [
    path("",vIEws.IndexVIEw.as_vIEw(),name="index"),path("<int:pk/",vIEws.DetailVIEw.as_vIEw(),name="detail"),path("<int:pk/results/",vIEws.ResultsVIEw.as_vIEw(),name="results"),path("<int:question_ID>/Vote/",vIEws.Vote,name="Vote" ),]

vIEws.py

from django.http import httpResponseRedirect
from django.shortcuts import get_object_or_404,render
from django.urls import reverse
from django.vIEws import generic

from .models import Choice,Question
# Create your vIEws here.
class IndexVIEw(generic.ListVIEw):
    template_name = "polls/index.HTML"
    context_object_name = "latest_question_List"

    def get_queryset(self):
        return Question.objects.order_by("-pub_date")[:5]

class DetailVIEw(generic.DetailVIEw):
    model = Question
    template_name = 'polls/detail.HTML'

class ResultsVIEw(generic.DetailVIEw):
    model = Question
    template_name = 'polls/results.HTML'

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):
        return render(request,"polls/detail.HTML",{"question": question,"error_message": "You dIDn't select a choice",})
    else:
        selected_choice.Votes += 1
        selected_choice.save()   
        return httpResponseRedirect(reverse("polls:results",args=
        (question.ID,))) 

index.HTML

{% if latest_question_List %}
    <ul>
    {% for question in latest_question_List %}
        <li><a href="{% url 'polls:detail' question.ID %}">{{ question.question_text }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}

解决方法

在 urls.py 中,您缺少捕获参数所需的右括号:

path("<int:pk/",views.DetailView.as_view(),name="detail"),

必须改为:

path("<int:pk>/",

这适用于 urls 文件中的“详细信息”和“结果”视图。

大佬总结

以上是大佬教程为你收集整理的为什么我在 Django 3.2全部内容,希望文章能够帮你解决为什么我在 Django 3.2所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: