问题
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