I am working on a python django website and my urls are not working properly. I have included my urls.py and the nav links which are the same on each page.
Urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name="home"),
path('products/', views.products, name='products'),
path('customer/<str:pk_test>/', views.customer, name="customer"),
]
navbar.html:
{% load static %}
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<img src="{% static 'images/logo.png' %}">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="{% url 'home' %}">Dashboard</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'products' %}">Products</a>
</li>
</ul>
</div>
</nav>
With minimum information provided, I can come to understand that you facing problem to visit another link or url using your navbar despite providing correct information. If that is the case, I think this can solve your issue.
Check your class nav-link :
<a class="nav-link" href="{% url 'products' %}">Products</a>
You must also provide the namespace of your application from the settings urls.py like this:
`<a class="nav-link" href="{% url 'app_namespace:products' %}">Products</a>`
This is the reference from documentation. https://docs.djangoproject.com/en/3.1/topics/http/urls/#url-namespaces
Related
I have problem when I visit localhost:8000/home:
Powershell: django.urls.exceptions.NoReverseMatch: Reverse for 'register' not found. 'register' is not a valid view function or pattern name.
html:
<section>
<nav class="navbar navbar-expand-lg bg-dark navbar-dark ">
<!-- Container wrapper -->
<div class="container-fluid justify-content-center">
<!-- Navbar brand -->
<a class="navbar-brand" href="{% url 'home_page' %}"> Turbine Power Web</a>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ms-auto">
{% if user.is_authenticated %}
<li class="nav-item"><a class="nav-link" href="{% url 'accounts:logout' %}">Logout</a></li>
<li class="nav-item"><a class="nav-link" href="{% url 'accounts:password_change' %}">Change Password</a></li>
{% else %}
<li class="nav-item"><a class="nav-link" href="{% url 'accounts:login' %}">Login</a></li>
<li class="nav-item"><a class="nav-link" href="{% url 'accounts:user-registration' %}">Register</a></li>
{% endif %}
<li class="nav-item"><a class="nav-link" href="">Turbine Models</a></li>
<li class="nav-item"><a class="nav-link" href="">Author</a></li>
{% if user.is_company %}
<li class="nav-item"><a class="nav-link" href="">My Models</a></li>
{% endif %}
<li class="nav-item"> Hello, {{ user.username|default:'Guest' }} </li>
</ul>
</div>
</div>
</nav>
<!-- Navbar -->
</section>
views.py
def home_page(request):
return render(request, 'turbineweb/home_page.html')
project urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('accounts.urls', namespace='accounts')),
path('', include('turbineweb.urls')),
accounts urls.py
app_name = 'accounts'
urlpatterns = [
path('login/', CustomLoginView.as_view(redirect_authenticated_user=True, template_name='accounts/login.html',
authentication_form=LoginForm), name='login'),
path('logout/', auth_views.LogoutView.as_view(template_name='accounts/logout.html'), name='logout'),
path('registration/', RegisterView.as_view(), name='users-registration'),
path('password-change/', ChangePasswordView.as_view(), name='password_change'),
path('password_reset/', auth_views.PasswordResetView.as_view(), name='password_reset'),
path('password_reset/done/', auth_views.PasswordResetDoneView.as_view(), name='password_reset_done'),
path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
path('reset/done/', auth_views.PasswordResetCompleteView.as_view(), name='password_reset_complete'),
]
turbineweb urls.py
app_name = 'turbineweb'
urlpatterns = [
path('home/', views.home_page, name='home_page'),
]
Error:
You made a typo, it is users-registration and not user-registration :
<li class="nav-item"><a class="nav-link" href="{% url 'accounts:users-registration' %}">Register</a></li>
I have created a 2nd app in my django project and I am trying to create a HTML files that contains links to both the main app and the 2nd app.
I am getting this error:
Reverse for 'deploy' not found. 'deploy' is not a valid view function or pattern name.
My code is:
urls.py
from django.conf.urls import url
from django.contrib import admin
from django.urls import path, include
from applications.atoll_queue_manager_fe_project.fe import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
path('create/', views.createtodo, name='createtodo'),
# App2 - Deploy Side Branch
path('deploy-side-branch/', include('fe2.urls')),
]
2nd app fe2/urls.py
from django.urls import path
from applications.fe2 import views
app_name = 'fe2'
urlpatterns = [
path('', views.deploy_side_branch, name='deploy'),
]
navbar section in base.html (extended by new.html)
<ul class="navbar-nav mr-auto">
<li class="nav-item {{ deploy }}">
<a class="nav-link" href="{% url 'deploy' %}">Deploy</a>
</li>
<li class="nav-item {{ current }}">
<a class="nav-link" href="{% url 'currenttodos' %}">Current</a>
</li>
<li class="nav-item {{ completed }}">
<a class="nav-link" href="{% url 'completedtodos' %}">Completed</a>
</li>
<li class="nav-item {{ create }}">
<a class="nav-link" href="{% url 'createtodo' %}">Create</a>
</li>
</ul>
use app-name along with name in href. Do this:
<a class="nav-link" href="{% url 'fe2:deploy' %}">Deploy</a> <!-- add <app_name>:<url_name> -->
instead of this:
<a class="nav-link" href="{% url 'deploy' %}">Deploy</a>
This is the way to reference a URL from a 2nd app. The 2nd app is namespace, so using URLs from fe2/urls.py requires this line in base.html
<ul class="navbar-nav mr-auto">
<li class="nav-item {{ deploy }}"> <<<<<<------- Line to modify. Include namespace
<a class="nav-link" href="{% url 'deploy' %}">Deploy</a>
</li>
to look like this
<li class="nav-item {{ fe2:deploy }}">
and
<a class="nav-link" href="{% url 'fe2:deploy' %}">Deploy</a>
I appreciate that similar questions have been asked before, but after looking at them, I still cannot seem to solve my issue.
By following the documentation, I have the below files:
views.py
from django.shortcuts import render
# Create your views here.
def base(request):
articles = 'articles'
dboard = 'dashboard'
pages = {'dboard':dboard, 'Articles':articles}
return render(request, 'collatedata/base.html', pages)
def dash(request):
return render(request, 'collatedata/dash.html')
collatedata/urls.py:
urlpatterns = [
path('', views.dash, name='dash'),
]
main urls.py:
urlpatterns = [
path('', include('collatedata.urls')),
path('admin/', admin.site.urls),
]
base.html:
...
<li class="nav-item">
<a href="#" class="nav-link text-white p-3 mb-2">
<i class="fas fa-home text-light fa-lg mr-3"></i>
{{ dboard }}
</a>
</li>
...
dash.html
{% extends "./base.html" %}
however, the output does not read {{ dboard }}
so returns this:
<li class="nav-item">
<a href="#" class="nav-link text-white p-3 mb-2">
<i class="fas fa-home text-light fa-lg mr-3"></i>
</a>
</li>
could it be to do with base.html being extended?
How To Fix Auto login When I go to this URL like this: 127.0.0.1:8000/profile/1 When I see If Someone go to that URL so Django login him without password and username
ERROR:
How To Fix Auto Login In Django
You Can See The Gif For More Details:
Views.py
def profile_detail(request,pk):
user = get_object_or_404(User, pk=pk)
model = user_register_model()
return render(request,'profile_detail_view.html',{'user':user,'model':model,})
urls.py
from . import views
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', views.index,name='index'),
path('accounts/signup/', views.user_reg,name='register'),
path('profile/<int:pk>',views.profile_detail,name='profile'),
]
Here is my Base.html
<ul class="navbar-nav ml-auto">
{% if not user.is_authenticated %}
<li class="nav-item">
<a class="nav-link navaour" href="{% url 'register' %}"><i class="fa fa-check-square-o"></i> Sign up Free</a>
</li>
<li class="nav-item">
<a class="nav-link navaour" href="{% url 'login' %}"><i class="fa fa-user"></i> Login</a>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link navaour" href=""><i class="fa fa-user"></i> Profile</a>
</li>
<li class="nav-item">
<a class="nav-link navaour" href="{% url 'logout' %}"><i class="fa fa-power-off"></i> Logout</a>
</li>
{% endif %}
Here is my profile_detail_view.html
<div class="row">
<div class="col-sm-3 col-md-2 col-5">
<label style="font-weight:bold;">Full Name</label>
</div>
<div class="col-md-8 col-6">
{{user.username}}
</div>
</div>
Any Help Appreciated
Thanks!
replace {% if not user.is_authenticated %} with {% if not request.user.is_authenticated %} in your template. You missed request.
you can also use #login_required before the def in views.py and importing library
from django.contrib.auth.decorators import login_required
This will help in blocking those views of url for the users who aren't logged in.
I am using Django and integrated Bootstrap with Django. Here is my HTML code for the navigation bar:
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Project name</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li >About</li>
<li>Contact</li>
<li class="dropdown">
Games <span class="caret"></span>
<ul class="dropdown-menu" role="menu">
<li>RacingDNA</li>
<li>Skater Game</li>
</ul>
</li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
I have written the CSS for an active navigation bar also. Here, only one navigation bar is active. I want to make the clicked navigation bar active and thus apply my CSS. My CSS is working perfect for active navigation bar and for this situation only for one.
I googled and found a solution to add this jQuery:
$('.nav.navbar-nav > li').on('click', function (e) {
e.preventDefault();
$('.nav.navbar-nav > li').removeClass('active');
$(this).addClass('active');
});
Now here is where I am stuck. I don't know where to write this jQuery.
I put this file in the static/js folder and named this code nav-bar.js. However, there is no improvement. Where am I going wrong and where am I making mistakes?
If you dont want to send any additional context from views then you can handle it like this with resolver_match:
<li {% if request.resolver_match.url_name == 'home' %}class="active"{% endif %}>
HOME
</li>
where 'home' is the name given to your url pattern for / (probably '^$') in your urls.py.
So obviously to use this you need to name all your url patterns, which is a good practice anyway.
This solution didn't work when the href attribute of your links will be different from href="#". Why ? Simply because each click on a link triggers a request to the server. In your JS code, you add the method preventDefault() in order to avoid this basic behavior, but I think that not really your objective for this purpose, isn't it ?
To handle this kind of feature you can update your template code by adding something like this :
base.html
<div class="collapse navbar-collapse" id="tn-navbar-collapse">
<ul class="nav navbar-nav">
<li class="{% if nbar == 'home' %}active{% endif %}">
HOME
</li>
...
</ul>
</div>
views.py
def your_view(request):
...
return render(request, 'yourtemplate.html', {'nbar': 'home'})
With this way, you don't have to manage this feature with javascript.
Alternative including URL namespaces:
<li class="nav-item {% if request.resolver_match.view_name == 'scores:list' %}active{% endif %}">
<a class="nav-link" href="{% url 'scores:list' %}">Scores</a>
</li>
I recently ran into this problem and tried every solution I could find. Eventually, I came to this. This example assumes the path to this content is yoursite.com/about
Give your link an id.
<li><a id="about" href="#">About</a></li>
var path = $(location).attr('pathname')
var pa = path.split("/")
$('#'+pa[1]).tab('show')
#rphonika has give a nice answer. But if you have many navigation menus then adding template logic in each list item makes the code untidy. Menu items can be assigned a unique id, the id passed in views and active class can be assigned using a one liner javascript code in html itself.
base.html
<ul class="nav navbar-nav navbar-left">
<li><a class="navmenu-a" id="overview" href="{% url 'webapp:overview' %}">Overview</a></li>
<li><a class="navmenu-a" id="plot" href="{% url 'webapp:plot' %}">Plot</a></li>
<li><a class="navmenu-a" id="board" href="{% url 'webapp:board' %}">Board</a></li>
<li><a class="navmenu-a" id="storyboard" href="{% url 'webapp:storyboard' %}">Storyboard</a></li>
<li><a class="navmenu-a" id="freqs" href="{% url 'webapp:freqs' %}">Faq</a></li>
</ul>
.
.
.
<script type="text/javascript">
{% if nmenu %} $("a#{{nmenu}}").addClass("navmenu-a-active") {% endif %}
</script>
views.py
def overview(request):
...
return render(request, 'overview.html', {'nmenu': 'overview'})
def plot(request):
...
return render(request, 'plot.html', {'nmenu': 'plot'})
And so on for other views
I think the easiest solution is JavaScript / CSS:
Anyway, here is the solution using Jquery. Notice n1,n2 classes in html
In base.html
<nav>
<ul class="nav flex-column">
<li class="nav-item">
<a class="nav-link n1" href="{% url 'polls:dashboard' %}">
<span data-feather="home"></span>
Dashboard
</a>
</li>
<li class="nav-item">
<a class="nav-link n2" href="{% url 'polls:restaurants' %}">
<span data-feather="file"></span>
Restaurants
</a>
</li>
</ul>
</nav>
<script>
$('nav').find('{% block active_link%}{%endblock%}').addClass('active')
</script>
Then in your dashboard template:
{% block active_link %}.n1{%endblock%}
In your restaurants template:
{% block active_link %}.n2{%endblock%}
After adding gamer's JavaScript into your template code_here , you got to make sure you have the similar settings in your CSS file also, in order to make JavaScript work.
.navbar-nav > li.active > a {
color: #d74b4b;
background: transparent;
border-bottom: none;
}
I would rather prefer to only edit the template using rather than editing both views and templates for this. People needing the same, rather than following the accepted answer, may consider the below way:
<ul class="navbar-nav">
<li class="nav-item {% ifequal request.path '/' %} active {% endifequal %}">
<a class="nav-link" href="/">Home</a>
</li>
</ul
You can change 'index' above with your own
You can check if a string is in your URL path. This is helpful for hierarchical or nested URL paths
<li {% if 'home' in request.get_full_path %}class="active"{% endif %}>
HOME
</li>
If you want activate by context you can do like this variable == to context in .views
<li class="nav-item {% ifequal variable context %}active{% endifequal%}">
<a class="nav-link" href="{% url 'scores:list' %}">Scores</a>
</li>