Reputation: 381
Maybe this question has been answered before in one way or another. But I need to ask because I tried to answer it myself I googled it for days but I couldn't figure it out.
I have an app called reception in my Django project.
I figured out everything but this.
I have the link in the html directing to the right urlpattern.
<p><tab1><a href="{% url 'go_to_reception' %}">Reception</a></tab1></p>
urlpatterns = [..., path('go_to_reception', views.reception, name='go_to_reception'), ...]
And the view is like this
def reception(request):
return render(request, 'reception.html')
now instead of reception.html I want the link to go the reception page. The app page. What do I need to write there. I have tried everything that came to my mind but nothing worked.
the reception is as follows:
http://127.0.0.1:8000/admin/reception/
So how do I point to this.
Please help! Thanks
Upvotes: 0
Views: 176
Reputation: 71
So im just going to write the solution that we discussed in the chat:
{% url "admin:app_list" app_label="reception" %}
Upvotes: 1
Reputation: 71
have you got a view called go_to_rececption in your reception.views or a url named go_to_reception in your reception.urls?.
and no worries just trying to help!!!
Upvotes: 0
Reputation: 381
Thank you for your answer Matthew,
After your latest comment, I have changed my code to the following:
<p><tab1><a href="{% url 'reception:go_to_reception' %}">Reception</a></tab1></p>
and in url.py its:
path('reception/', include('reception.urls', namespace='reception-urls')),
in reception.urls.py
app_name = "reception"
and the message now says:
django.urls.exceptions.NoReverseMatch: Reverse for 'go_to_reception' not found. 'go_to_reception' is not a valid view function or pattern name.
I am sorry for going back and forth like this, but I honestly can't make it work.
Upvotes: 0
Reputation: 16
Look, this help you? i solve a similar problem in this way
url.py
path('gotoreception', views.reception, name="go_to_reception"),
html
<a href="{% url 'go_to_reception' slug=slug %}"
model
slug = models.SlugField(max_length=255, unique=True)
view
def home(request, slug):
context = {'yourdata':yourdata}
return render(request,'index.html', context)
On the template tag url you need use the name of your path, you can use a slug if you store the field on your db
3º option but not recommended, you can add the value if you have on a variable but is not a good practice
I recommend you this 2 resources:
hardcoded way
can you test this? urls
urlpatterns = [..., path('reception/', views.reception, name='go_to_reception'), ...]
html
and on the link <a href="127.0.0.1/reception/">
is the hardcoded way but in the future you can have troubles or relative urls, 404 on internal pages
Upvotes: 0