I am trying to set up some URLs for tags and categories on a forum. The links look like this
<a href="{% url 'article-list-by-tag' %}{{ tag }}" class="badge badge-info" >{{ tag }}</a>
The URL pattern article-list-by-tag comes from a DjangoCMS app called AldrynNewsblog. It can be found HERE and looks like this
url(r'^tag/(?P<tag>\w[-\w]*)/',
TagArticleList.as_view(), name='article-list-by-tag'),
Note that the NewsBlog does not have app_name declared in its urls.py.
The app itself is part of a larger Django app called DjangoCMS. The url entry-point for the latter is declared in project's main urls.py as follows:
path('', include('cms.urls')),
The error that I am getting is
Reverse for 'article-list-by-tag' not found. 'article-list-by-tag' is not a valid view function or pattern name.
What should I do to resolve this? I have tried adding namespace to various URL entries as well as app_name to NewsBlog to no avail. The Django version being used is 2.1
You need to pass the .slug of the tag as parameter of the URL, so:
<a href="{% url 'article-list-by-tag' tag=tag.slug %}" class="badge badge-info" >{{ tag }}</a>
If you import it with a namespace, you need to prefix the name of the view with that prefix, so 'some_namespace:article-list-by-tag'.
In case it helps anyone, the following code worked for my use-case:
<a href="{% namespace_url 'article-list-by-tag' tag=tag.slug %}" class="badge badge-info" >{{ tag }}</a>
Where the namespace_url is a template tag provided by DjangoCMS.
Related
For starters, i have basically no experience with coding, i followed a tutorial on how to code a website and when i tried to develope the idea for my own purposes i can't seem to get anywhere.
{% if user.is_authenticated %}
<a class="nav-item nav-link" id="home" href="/">Home</a>
<a class="nav-item nav-link" id="logout" href="/logout">Logga ut</a>
<a class="nav-item nav-link" id="öppettider" href="/öppettider">Öppettider</a>
{% else %}
<a class="nav-item nav-link" id="login" href="/login">Logga in</a>
<a class="nav-item nav-link" id="sign-Up" href="/sign-up">Registrera</a>
{% endif %}
the navbar item "öppettider" is supposed to direct to a HTML in python where i intend to display a picture, atleast this is how i thought it would work. this resulted in 404 not found.
The other navbar items have their seperate .HTML files so i figured that navbar refers to .HTML's
Essentially i tried to expand a simple "post notes" website to be more of a "presentation" website.
By creating a new .HTML in templates and creating a navbar item for this .HTML.
According to your code, it seems that you are using flask or django.
When you go on a URL in django or flask, it does not point directly to html template. It first checks all the urlpatterns provided, which in the case of flask are written along with the views. A view is the functions which will be called if a certain URL is being called. That function decides what to do next. To render a html template, you'll have to return the template from that view, and ONLY then, your html template is rendered.
As you have said that you just created a new HTML file and a url in navbar, that will not just work. You'll have to create those views and urlpatterns for the program to know which template to go on, or more precisely what to do next after reaching that url.
Flask
If you are using flask, you must add your view for this particular URL
# A basic view example without any params
#app.route('/öppettider')
def öppettiderView():
return render_template('öppettiderTemplate.html')
Django
If you are using django, you must change your views.py and urls.py accordingly
urls.py
...
urlpatterns = [
...
path('öppettider/', views.öppettiderView, name='members'),
]
views.py
...
# A basic view example without any params
def öppettiderView(request):
return render(request, 'öppettiderTemplate.html', context)
I know that in a template file I can include this code which will return a list of links
{% for q in all %}
<ul>
<li><a href={% url 'detail' q.id %}>{{ q.question_text }}</a></li>
</ul>
{% endfor %}
Now django will search for the name 'detail' in the urls.py file of my app directory and it will automatically give the value of q.id to that argument. But what if I have a url that contains more than 1 variable. So here I can only give one argument i.e, q.id. But what if I want to give more than one argument.
Hope I am clear
Well you can see the urls.py as a declaration of how URLs map to view functions (and class-based views). A url can have an arbitrary number of parameters.
If we have for example the following URL:
url(r'^detail/(?P<year>(\d+))/(?P<name>(\w+))/$', views.detail),
We can see this url as some sort of virtual function that would take parameters like:
def some_url(year, name):
# ...
pass
So we can construct this URL with unnamed parameters:
{% url 'detail' 1981 q.name %}
or with named parameters:
{% url 'detail' name=q.name year=1981 %}
I have a header in my webpage and a logo in that header. I want to insert an anchor tag in that logo which points to my home page which is index.html page,despite which ever view or page is called. I have created a base.html,a snippet of which is:
<div class="header">
<div class="inner_header">
<div class="logo">
<a href='#'><img src="{% static 'images/logo.png' %}" alt="logo" /> </a>
</div>
I have extended this base.html to all my other template files
My url pattern that points to my index page and to other pages is this:
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^category/(?P<category_name_slug>[\w\-]+)/$', views.category, name='category'),
url(r'^latest/(?P<latest_title_slug>[\w\-]+)/$', views.latest, name='latest'),
url(r'^headline/(?P<heading_title_slug>[\w\-]+)/$', views.headline, name='headline'),)
I have tried using <a href="index.html">,<a href=''> and bunch of other things which doesn't produce the desired result or gives an error. What is the appropriate way to do this without writing lot of codes ?
Rohit Jain's suggestion of using the url template tag is correct
<a href="{% url 'index' %}">
But it might help to understand what your current approaches are doing
<a href='#'> - This is trying to link to an id element on your page, but since you haven't provided an id name it just refers to the page itself. It doesn't redirect anywhere
<a href="index.html"> - You are trying to refer to the template directly, to which you don't have a urlpattern that maps this url to a view that would then display that template
<a href=''> - This is pretty much the same as using # since you're not changing the the link you're appending to
You may have some success with <a href="/"> but again, this isn't the correct way to do it in django since you may decide in the future to change what url pattern matches up to a particular view (i.e you decide to add i18n patterns) so you should always try to use the url template tag.
I learn Python, doing test project.
Case: I need to use class="active" for <li> based on page url. Now I use django.core.context_processors.request to take url from path.
How it looks now:
<li role="presentation" {% if request.path == '/' %}class="active"{% endif %}>Home</li>
<li role="presentation" {% if '/groups' in request.path %}class="active"{% endif %}>Groups</li>
So, if I open main page - first <li> is active, if url contains "/groups" - secon <li> is active. And it works, but I want to do it more complex, comparing url with url.patterns in urls.py:
url(r'^$', 'students.views.students_list', name='home'),
url(r'^groups/$', 'students.views.groups_list', name='groups')
Question: how to do it? I can't just use {% url 'groups' %} in if-statement because of syntax error. Need some advice.
You can assign the result of url tag to the variable.
{% url 'groups' as groups_url %}
# use groups_url variable somewhere
I'm following the Django tutorial, and in the third part, they create a link in the template. Now, I followed and wrote a template like this:
<ul>
{% for blog in blogs %}
<li>
<a href="{% url 'detail' blog.id %}">
<h1>{{ blog.name }}</h1>
</a>
<p>{{ blog.description }}</p>
</li>
{% endfor %}
</ul>
And I have set the urls.py like this:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<blog_id>[0-9]+)/$', views.detail, name='detail'),
]
However, when I point my browser to the index, it gives me the following error:
NoReverseMatch at /
Reverse for 'detail' with arguments '(1,)' and keyword arguments '{}' not found.
1 pattern(s) tried: ['$(?P<blog_id>[0-9]+)/$']
The error details highlight this part of the template:
<a href="{% url "detail" blog.id %}">
What's going on in here? How do I fix this? I'm using Django 1.8.4.
You are mispelled in your main urls.py, something like.
url(r'^polls/$', include('polls.urls')),
You have to write like this
r'^polls/'. # Note please remove `$` in end