我是使用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
感谢。
答案 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