Navbar doesn't direct to a URL - python

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)

Related

NoReverseMatch for Django and DjangoCMS App

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.

How to call urls.py fetching a value from a loop in Django

On my index page, I have an image gallery. When someone clicks on an image, it should show more information with more photos in another page. All are loading from MySQL database using a for loop. I'm unable to get the detail of clicked image from my data base. It loads all the data
This page's process is like a news website -- all the news loading from a loop. if someone clicks on a news item it should only show details about the clicked item.
Below is my index.html page, my urls.py and views.py source code also.
I'm using Python, Django with MySQL; all latest versions.
Home page, source code of my images gallery
{% for x in destination %}
<!-- Destination -->
<a href="destination" id="{{x.id}}"><div class="destination item" >
<div class="destination_image">
<img src="{{x.img.url}}" alt="">
{% if x.offer %}
<div class="spec_offer text-center"><a >Special Offer</a></div>
{% endif %}
</div>
<div class="destination_content">
<div class="destination_title">{{x.name}}</div>
<div class="destination_subtitle"><p>{{x.short_description}}</p></div>
<div class="destination_price">From ${{x.price}}</div>
</div>
</div></a>
{% endfor %}
from . import views
from django.urls import path
urlpatterns = [
path('destination', views.destination, name='destination'),
path('', views.index, name='index')
]
from django.shortcuts import render
from .models import Destination
def destination(request):
dest = Destination
return render(request, 'destination.html', {'sub_destination': dest})
You can use Reverse resolution of URLs. just add another url pattern in your urls.py:
urlpatterns = [
path('destination/<int:dest>/', views.destination_content, name='destination_content'),
# ...
]
and use it inside your for loop in template like this:
<div class="destination_content">
<div class="destination_title">{{x.name}}</div>
...
</div>
NOTE: you must define destination_content function in your views.py

django template anchor tag not working

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.

Django - Include app template in base template

I trying to include app result in base template. Template is displayed properly but I don't see any results. When I open app url directly (127.0.0.1/Project/config/) it show results.
Project / urls.py
urlpatterns = patterns('',
url(r'^$', 'Project.views.index'),
url(r'^config/', DisplayChangelog),
)
config / views.py
from config.models import Changelog
def DisplayChangelog(request):
changes = Changelog.objects.all()
t = loader.get_template("changelog.html")
c = Context({'changes': changes})
return HttpResponse(t.render(c))
templates / base.html
...
<ul class="nav navbar-nav">
{% include "changelog.html" %}
</ul>
...
templates / changelog.html
...
{% for change in changes %}
<li class="media">
<div class="body">
{{ change.desc }}
<div class="date">{{ change.date }}</div>
</div>
</li>
{% endfor %}
...
First of all you have to put some variables in your base.html if you use them on every single page, but also you have to pass these variables somehow. Of course you won't add one more object to the context in each view so there is an idea of context processors. Take a look at the docs if this is really what you need.
In your case, I think it's just enough to add a changes variable to the index view's context and include your changelog in the template rendered by index view.
So basically I want to say that you have to pass objects to the view's context if you want to use them in your templates, even if you use them in included templates.
You can try this after you define "changes" variable in your view:
{% include "changelog.html" with changes=changes %}

Navigation in Django, using url.patterns

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

Categories

Resources