NoReverseMatch at / <url>渲染表单时

时间:2018-01-01 12:44:14

标签: python django

我对Django比较新,我试图使用Django表单将数据插入到表中,但是我遇到了这个错误:

/ vinesF / NewVid的NoReverseMatch
反向&#39; NewVid&#39;未找到。 &#39; NewVid&#39;不是有效的视图函数或模式名称。

以下是我的观点:

def NewVid(request):
    if request.POST:
        form = AddVidForm(request.POST)
        if form.is_valid():
            primaryName = form.cleaned_data['primaryName']
            Origin = form.cleaned_data['Origin']
            PreferredAudience = form.cleaned_data['PreferredAudience']
            Date = form.cleaned_data['Date']
            Link = form.cleaned_data['Link']
            VPath = form.cleaned_data['VPath']
            AddPath = form.cleaned_data['AddPath']

            FreshVid = video(primaryName=primaryName, Origin=Origin, PreferredAudience=PreferredAudience, Date=Date,Link=Link, VPath=VPath, AddPath=AddPath)

            FreshVid.save()

            return HttpResponse("New Video added")
        else:
            return render(request,'vine/Newvid.html', {form:'form'})
    else:
        form = AddVidForm()
        return render(request,'vine/Newvid.html', {form:'form'})

HTML模板代码:

<form method="post" action="{% url 'NewVid' %}" >
    {% csrf_token %} <!-- This line inserts a CSRF token. -->
    <table>
        {{ form.as_table }} <!-- This line displays lines of the form. -->
    </table>
    <p><input type="submit" value="Create" /></p>
</form>
{% endblock %}

网址处理程序:

app_name = 'vinesF'
urlpatterns = [
    url(r'^NewVid$',views.NewVid, name='NewVid'),
]

感谢任何帮助。

3 个答案:

答案 0 :(得分:1)

可能是代码:

url(r'^NewVid$',views.NewVid, name='NewVid'),

更改为:

url(r'^NewVid/$',views.NewVid, name='NewVid'),

并改变:

return render(request,'vine/Newvid.html', {form:'form'})

为:

return render(request,'vine/NewVid.html', {form:'form'})

答案 1 :(得分:0)

我终于通过以下方式解决了这个问题: 首先,我将我的url处理程序更改为直接从根目录拉到app目录:

urlpatterns = [
url(r'^vinesF/',include('vinesF.urls')),
url(r'^admin/', admin.site.urls),
url(r'^NewVid',vinesF.views.NewVid, name='NewVid'),

]
并在视图中进行更正,我错误地颠倒了解析:

else:
        return render(request,'vine/Newvid.html', {'form':form})
else:
    form= AddVidForm()
    return render(request,'vine/Newvid.html', {'form':form})

答案 2 :(得分:-1)

在代码格式化中,我看到第二行没有缩进。但是,如果这是错误,它应该给你一个缩进错误。

def NewVid(request):
    if request.POST:
        form= AddVidForm(request.POST)
        if form.is_valid():
            primaryName= form.cleaned_data['primaryName']
            Origin= form.cleaned_data['Origin']
            PreferredAudience= form.cleaned_data['PreferredAudience']
            Date= form.cleaned_data['Date']
            Link= form.cleaned_data['Link']
            VPath= form.cleaned_data['VPath']
            AddPath= form.cleaned_data['AddPath']

            FreshVid= video(primaryName=primaryName, Origin=Origin, PreferredAudience=PreferredAudience, Date=Date,Link=Link, VPath=VPath, AddPath=AddPath)

            FreshVid.save()

            return HttpResponse("New Video added")
        else:
            return render(request,'vine/Newvid.html', {form:'form'})
    else:
        form= AddVidForm()
        return render(request,'vine/Newvid.html', {form:'form'})