python - django里显示NoReverseMatch

查看:109
本文介绍了python - django里显示NoReverseMatch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

各位大虾好,小虾在views.py里添加了一个函数,在网页端显示就是下面的错误:

我的urls.py是下面的样子,Online是我的app名称:

from django.conf.urls import url,include
from django.contrib import admin
import Online.views
admin.autodiscover()
from Online import views as Online_views
    
urlpatterns = [
    url(r'^admin/',admin.site.urls),
    url(r'^lists/(?P<table>\w+)/$',Online.views.lists,name='lists'),
    url(r'^add/(?P<table>\w+)/$',Online.views.add,name='add'),
    #基础资料的显示
    #修改数据,?P<pk>\d+代表传过来的id值,且id值一定为数字
    url(r'^edit/(?P<table>\w+)/(?P<pk>\d+)/$',Online.views.edit,name='edit'),
    #删除数据
    url(r'^delete/(?P<table>\w+)/(?P<pk>\d+)/$',Online.views.delete,name='delete'),
    url(r'^index/',Online.views.index,name='index'),
    url(r'^login/',include('Online.urls')),
]

我的views.py是下面的样子:

def edit(request,table,pk):
    if table == 'alionlineECS':
        table_ins = get_object_or_404(alionlineECS,pk=pk)
        #通过instance来将Form的数据做填充
        form = alionlineForm(request.POST or None,instance=table_ins)
        sub_title = '修改阿里云线上环境信息'
  
    #判断form是否有效
    if form.is_valid():
        #创建实例,需要做些数据处理,暂不做保存
        instance = form.save(commit=False)
        #将登录用户作为登记人,在修改时,一定要使用str强制
        if table == 'alionlineECS':
            instance.ecs_signer = str(request.user)
      
        #保存该实例
        instance.save()
        #跳转至列表页面,配合table参数,进行URL的反向解析
        return redirect('lists', table=table)

    context = {
        'table': table,
        'form': form,
        'page_title': '基础资料',
        'sub_title': sub_title,
    #与res_add.html用同一个页面,只是edit会在res_add页面做数据填充
    return render(request,'res_add.html',context)

lists对应的函数是这样的:

def lists(request,table):
        #不同的需求跳到不同的界面
    if table == 'alionlineECS':
        data = alionlineECS.objects.all()
        list_template = 'alionlineECS_list.html'
        sub_title = '阿里云线上环境服务器'
        #建立一个context,将值传递到对应的页面
    context = {
        'data': data,
        'table': table,
        'sub_title': sub_title,
    }
        #跳转到相应页面,并将具体的值传递过去
    return render(request,list_template,context)

对应的alionlineECS_list.html是这样的:

{% extends "res_list.html" %}
{% block search %}
{% endblock %}

{% block table_tr %}
<th>云服务器名称</th>
<th>云服务器类型</th>
<th>云服务器内网地址</th>
<th>云服务器外网地址</th>
<th>云服务器外网带宽</th>
<th>云服务器配置</th>
<th>备注</th>
<th>登记人</th>

{% endblock %}
{% block table_td %}
<td>{{ item.ecs_name }}</td>
<td>{{ item.ecs_type }}</td>
<td>{{ item.ecs_inip }}</td>
<td>{{ item.ecs_outip }}</td>
<td>{{ item.ecs_ipwidth }}</td>
<td>{{ item.ecs_spec }}</td>
<td>{{ item.ecs_remarks }}</td>
<td>{{ item.ecs_signer }}</td>

{% endblock %}

使用了很多方法,比如把urls.py里的$去掉,或者是在reverse()中加入了app名称。都没有达到效果,还请各位指点迷津。

解决方案

你lists对应的view是Online.views.lists,代码呢?

这篇关于python - django里显示NoReverseMatch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆