如果路径正确,则找不到Django Page错误事件

时间:2018-09-03 06:54:56

标签: python django django-templates django-views django-urls

我是使用2.1版本的Django framework的新手,在urls.py中使用可选参数遇到麻烦

    from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('myapp/', include('myapp.urls')),
    path('myblog/', include('myblog.urls')),
    path('admin/', admin.site.urls),
]

Appname:myblog

urls.py

    from django.urls import include, path, re_path
from django.contrib import admin

from . import views
app_name = 'myblog' #FOR NAMESPACE ISSUE THIS IS REQUIRED
urlpatterns = [
    re_path(r'^test_view/<mode>/(?:blog-(?P<blog_id>\d+)/)?$', views.test_view)
]

myblog view.py

def test_view(request, mode, blog_id):
    return HttpResponse('ss')

myblog模板

<button type="button" class="btn-xs btn-primary" onclick="location.href='{% url 'myblog:test_view' 'create' 1 %}'">Create New Blog</button>

错误

IN TEMPLATE : 
NoReverseMatch at /myblog/
Reverse for 'test_view' not found. 'test_view' is not a valid view function or pattern name.

直接访问http://127.0.0.1:8000/myblog/test_view/create/1/时我获得了网址   PAGE NOT FOUND

请注意,根据DJANGO DOCUMENTATION中提供的文档,我进行了相同的更改,但仍然出现错误

感谢。

1 个答案:

答案 0 :(得分:1)

您可以使用import * as React from 'react'; import './SoftwareLicenseCodes.css'; interface SoftwareLicenseCodesProps { } interface SoftwareLicenseCodesState { count: string; oneTimeUsage: boolean; duration: string; validFrom: string; validTo: string; distributor: string; } class SoftwareLicenseCodes extends React.Component<SoftwareLicenseCodesProps, SoftwareLicenseCodesState> { constructor(props: SoftwareLicenseCodesProps) { super(props); this.state = { distributor: '', count:'', oneTimeUsage: false, duration: '', validFrom: '', validTo: '' }; this.onInputChange = this.onInputChange.bind(this); } handleSubmit(event: React.FormEvent<HTMLFormElement>) { alert('submit'); event.preventDefault(); } onInputChange = (event: React.FormEvent<HTMLInputElement>) => { const value = event.currentTarget.type === 'checkbox' ? event.currentTarget.checked : event.currentTarget.value; this.setState({ [name]: value }); } render() { return ( <div className="user-container software-codes"> <div className="user-single-container"> <h1>Software License Codes</h1> <form className="software-codes__form" onSubmit={this.handleSubmit}> <label> <span className="software-codes__input-element">Count</span> <input name="count" type="number" value={this.state.count} /> </label> <label> <span className="software-codes__input-element">Distributor</span> <input name="distributor" type="text" value={this.state.distributor} /> </label> <label> <span className="software-codes__input-element">One time usage</span> <input name="oneTimeUsage" type="checkbox" checked={this.state.oneTimeUsage} /> </label> <label> <span className="software-codes__input-element">Duration</span> <input name="duration" type="number" value={this.state.duration} /> </label> <input className="software-codes__input-element" type="submit" value="Submit" /> </form> </div> </div> ); } } export default SoftwareLicenseCodes; 参数指定网址名称:

name