Django context does not render in base template - django

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?

Related

django.urls.exceptions.NoReverseMatch at /home/

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>

Django: active Home link

This is my first project with django, I wanted my static site html/css/js to turn it into a dynamic website. However, in navMenu I want to have 'home' link only if the user is not on the index page.
Here is my attempt:
<style>
#hm{
display:none;
}
.activate{
display:block;
}
</style>
<div id="navMenu" class='py px'>
<ul>
{% url 'home' as home_view %}
<li id = 'hm' {% if request.get_full_path != home_view%} class = 'activate' {% endif%}>Home</li>
<li class='brd'>Alumni</li>
<li class='brd'>Staff</li>
<li class='brd'>Services</li>
<li class='brd'>About</li>
<li><a id='btnSearch' href="#"><i class="fa fa-search searchUpdate"></i></a></li>
</ul>
</div>
the urls:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from pages.views import home_view
from events.views import events
urlpatterns = [
path('admin/', admin.site.urls),
path('', home_view, name = 'home'),
path('events/', events, name = 'events')
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
Any help is appreciated!
You could try adding a content block with and adding it only to the templates that you want to have the url on
If you dont know how to do that then it goes like this
<div id="navMenu" class='py px'>
{% block urls %}{% endblock urls %}
</div>
Then, for all of the pages that you want the url to be on, make a new template with:
{% block urls %}
<ul>
{% url 'home' as home_view %}
<li id = 'hm' {% if request.get_full_path != home_view%} class = 'activate' {% endif%}>Home</li>
<li class='brd'>Alumni</li>
<li class='brd'>Staff</li>
<li class='brd'>Services</li>
<li class='brd'>About</li>
<li><a id='btnSearch' href="#"><i class="fa fa-search searchUpdate"></i></a></li>
</ul>
{% endblock urls %}

why my Python Django urls is not working?

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

Django 3.0.3: NoReverseMatch at / : Reverse for 'post_new' with no arguments not found. 1 pattern(s) tried:

Need Help. I am not passing an argument while using href but still url pattern is looking for an argument.
Master urls.py
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'^$',include('portfolio.urls')),
re_path(r'accounts/login/$',LoginView.as_view(),name='login'),
re_path(r'accounts/logout/$',LogoutView,name='logout',kwargs={'next_page':'/'})
]
Portfolio.urls
urlpatterns = [
re_path(r'^$',views.PostListView.as_view(),name='post_list'),
re_path(r'^about/$', views.AboutView.as_view(),name='about'),
path ('post/new/',views.CreatePostView.as_view(),name='post_new'),
re_path (r'^drafts/$',views.DraftListView.as_view(),name='post_draft_list'),]
<nav class="navbar navbar-dark bg-dark">
<div class ="container">
<ul class= "navbar navbar-dark bg-dark">
<li><a class='navbar-brand' href="{% url 'post_list' %}"> My Blog </a>
<li>Github</li>
<li>Linkedin</li>
</ul>
<ul class='nav nsvbsr-right'>
{% if user.is_authenticated %}
<li>
New Post
Draft
Log Out
</li>
<li>
<a> Welcome : {{ user.username }} </a>
</li>
{% else %}
<li> <a class='nav nsvbsr-right' href="{% url 'login' %}"> Login </a> </li>
{% endif %}
</ul>
</div>
</nav>
I am getting the following error:
In Short: Page throws the error after user logs in (i.e. is_authenticated is true).
Models.py
class CreatePostView(LoginRequiredMixin, CreateView):
login_url="/login/"
redirect_field_name="portfolio/post_detail.html"
form_class = PostForm
model = Post
Remove the dollar from the URL pattern that includes portfolio.urls.
re_path(r'^',include('portfolio.urls')),
Or switch to path:
path('',include('portfolio.urls')),

How To Fix Auto Login In Django?

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.