Reverse for 'hobbieswithCSS.html' not found. 'hobbieswithCSS.html' is not a valid view function or pattern name (solved)

别等时光非礼了梦想. 提交于 2021-02-11 04:33:58

问题


I am trying to attach hobbieswithCSS.html file to my website, while using Django. I am a beginner when it comes to Django, so I have naturally came across some problems (in the title) like this.

I have this anchor tag on my homepage -

<a href="{% url 'basic_app:hobbieswithCSS.html' %}">My Hobbies</a>

I have this view in my views.py file -

def hobbieswithCSS(request):
    return render(request,'basic_app/hobbieswithCSS.html')

I think, that the main problem will be with the urlpatterns in urls.py file, because I am not sure how to set it up. These are my urlpatterns -

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^admin/', admin.site.urls),
    url(r'^basic_app/',include('basic_app.urls')),
    url(r'^logout/$',views.user_logout,name='logout'),
    url(r'^$', views.hobbieswithCSS, name='hobbieswithCSS'),
]

Could anybody please tell me, how could I change the code in order to make that hobbieswithCSS.html file display, when I am trying to run it on my server?

Thank You very much for any help.

EDIT: This question was originally a prequel of this question - TemplateDoesNotExist at /hobbies/ (solved)


回答1:


It must be:

<a href="{% url 'hobbieswithCSS' %}">My Hobbies</a>

Without basic_app.

Also you have the same path for index and hobbies . You have to change your url path:

urlpatterns = [
    url(r'^$', views.index, name='index'),
    ...
    url(r'^hobbies/$', views.hobbieswithCSS, name='hobbieswithCSS'),
]



回答2:


in your template update to

<a href="{% url 'basic_app:hobbieswithCSS' %}">My Hobbies</a>


来源:https://stackoverflow.com/questions/65956018/reverse-for-hobbieswithcss-html-not-found-hobbieswithcss-html-is-not-a-vali

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!