gpt4 book ai didi

python - Django,NoReverse 匹配

转载 作者:太空宇宙 更新时间:2023-11-04 03:46:40 25 4
gpt4 key购买 nike

所以我完成了我的应用程序并且有几个模型。此特定错误仅针对某些值出现。示例:每个用户都注册了一个类(class)和一年。因此,在一个页面中,我试图显示所选年份属于该类(class)的所有模块。当用户在 year 3 学习时,它可以工作。当它是 第 2 年 时,它会抛出该错误。

Reverse for 'module_details_upcoming' with arguments '(10L,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['studies/module/(?P<module_id>\\d)/upcoming/$']

在错误页面中它提示 {% url "module_details_upcoming"module.id %} 我猜也是 {% url "module_details_previous"module.id %} .

下面你可以看到我的代码:

View .py

def modules(request):
if request.user.is_authenticated():
user_id = request.user.id

try:
studying_course = Student.objects.get(user__id=user_id).course
studying_year = Student.objects.get(user__id=user_id).year
except Student.DoesNotExist:
return HttpResponseRedirect(reverse('step2'))

try:
modules_list = Module.objects.filter(course=studying_course,year=studying_year).exclude(name="school")
except Module.DoesNotExist:
modules_list = None
# import pdb; pdb.set_trace()
return render_to_response("studies/modules.html", {'course': studying_course, 'year': studying_year, 'modules': modules_list,}, RequestContext(request))

else:
return HttpResponseRedirect(reverse('home'))

def module_details_upcoming(request, module_id):
if request.user.is_authenticated():
user_id = request.user.id
dated = True

try:
studying_course = Student.objects.get(user__id=user_id).course
studying_year = Student.objects.get(user__id=user_id).year
except Student.DoesNotExist:
return HttpResponseRedirect(reverse('step2'))

try:
module = Module.objects.get(id=module_id)
except Module.DoesNotExist:
module = None

now = datetime.datetime.now()
events = Event.objects.filter(module__in=module_id,date__gte=now).order_by('date')

paginator = Paginator(events, 5)
try:
page = int(request.GET.get("page", '1'))
except ValueError:
page = 1

try:
posts = paginator.page(page)
except (InvalidPage, EmptyPage):
posts = paginator.page(paginator.num_pages)

return render_to_response("studies/module.html", {'module': module, 'events': events, 'posts': posts, 'dated': 'dated'}, RequestContext(request))
else:
return HttpResponseRedirect(reverse('home'))

网址.py

urlpatterns = patterns('',
url(r'^modules/$', 'studies.views.modules', name="modules"),
url(r'^module/(?P<module_id>\d)/upcoming/$', 'studies.views.module_details_upcoming', name="module_details_upcoming"),
url(r'^module/(?P<module_id>\d)/previous/$', 'studies.views.module_details_previous', name="module_details_previous"),
)

模板文件:

            {% if modules %}
<p>Below you can find all your modules for this year.</p>
<table class="table table-hover">
<th>Code</th>
<th>Name</th>
<th>Tasks/Events</th>
{% for module in modules %}
<tbody>
<td>{{ module.code }}</td>
<td>{{ module.name }}</td>
<td><a href="{% url "module_details_upcoming" module.id %}">Upcoming</a> / <a href="{% url "module_details_previous" module.id %}">Previous</a></td>
</tbody>
{% endfor %}
</table>
{% else %}
<p>Oops! It seems that there are no modules for {{ course.name }} in our records. Please contact your school office.</p>
{% endif %}

最佳答案

您需要在 url 标签中传递关键字参数 module_id:

{% url "module_details_upcoming" module_id=module.id %}

也看到这个类似的问题:NoReverseMatch with {% url ... %} and keyword args

UPD:您实际上是在 url 中捕获单个数字。由于 第 3 年 module_id10(两位数),因此无法完成反向操作。

替换:

url(r'^module/(?P<module_id>\d)/upcoming/$', 'studies.views.module_details_upcoming', name="module_details_upcoming"),
url(r'^module/(?P<module_id>\d)/previous/$', 'studies.views.module_details_previous', name="module_details_previous"),

with(观看 \d+ 而不是 \d):

url(r'^module/(?P<module_id>\d+)/upcoming/$', 'studies.views.module_details_upcoming', name="module_details_upcoming"),
url(r'^module/(?P<module_id>\d+)/previous/$', 'studies.views.module_details_previous', name="module_details_previous"),

关于python - Django,NoReverse 匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23451959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com