向现有项目中添加其他模板会出错 [英] Adding an additional template to an existing project errors out
问题描述
我上周发布了一些内容,但没有得到答案.我正在使用以下github项目来使用过滤器.我无法让其他模板正常工作.
您需要在应用程序的URL中包括您的应用程序URL.
urlpatterns = [url(r'^ $',TemplateView.as_view(template_name ='home.html'),name ='home'),url(r'^ search/$',FilterView.as_view(filterset_class = UserFilter,template_name ='search/user_list.html'),name ='search'),url(r'^ admin/',include(admin.site.urls)),url(r'^ myapp/',include('myapp.urls'),]
您还应该从应用程序的网址中删除 admin
之类的网址,否则它们将在/admin/
和/myapp/admin/代码>.
app_name ='myapp'urlpatterns = [url(r'^ results/$',views.results,name ='results'),]
建议(但不是必需)在应用程序的 urls.py
中设置 app_name
.如果这样做,则在反转网址时必须使用它,例如'myapp:results'
而不是'results'
.
I posted something last week and I haven't been able to get an answer. I'm using the following github project for using filters. I'm having trouble getting any additional templates to work.
https://github.com/sibtc/simple-django-filter
When I attempt to add a new template to the project or view I get the following error when trying to navigate to search because the template has a reference to a new page called results,which the results of the filter will have a POST occur:
django.urls.exceptions.NoReverseMatch: Reverse for 'results' not found. 'results' is not a valid view function or patter
n name.
When trying to add the url for the view for results using the following, I still get the error above:
url(r'^results/$', views.results, name='results'),
I also include from . import views in the urls.py
I'm adding an empty view for results:
def results(request):
return render(request, 'results.html', args)
I'm thinking it has to do something with how the directory structure of the simple filter app is setup and how i'm doing things.
I added the project URL below as well:
from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import TemplateView
from django_filters.views import FilterView
from mysite.search.filters import UserFilter
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name='home.html'), name='home'),
url(r'^search/$', FilterView.as_view(filterset_class=UserFilter, template_name='search/user_list.html'), name='search'),
url(r'^admin/', include(admin.site.urls)),
url(r'^results/', include('app_name.urls')),
]
Here are is application urls.py
from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import TemplateView
from django_filters.views import FilterView
from mysite.search.filters import UserFilter
from . import views
app_name = 'search'
urlpatterns = [
url(r'^results/$', views.results, name='results'),
]
Added installed apps:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'mysite',
'mysite.search',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'widget_tweaks',
]
Added directory structure:
You need to include your applications URLs in the project's urls.
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name='home.html'), name='home'),
url(r'^search/$', FilterView.as_view(filterset_class=UserFilter, template_name='search/user_list.html'), name='search'),
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include('myapp.urls'),
]
You should also remove the urls like admin
from the application's urls, otherwise they will be available at /admin/
and /myapp/admin/
.
app_name = 'myapp'
urlpatterns = [
url(r'^results/$', views.results, name='results'),
]
It's recommended, but not required, to set app_name
in the application's urls.py
. If you do this, then you'll have to use it when reversing the URLs, e.g. 'myapp:results'
instead of 'results'
.
这篇关于向现有项目中添加其他模板会出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!