django.urls.exceptions.NoReverseMatch at /home/ - django

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>

Related

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

no reverse matching at / and crawler is not a registered namespace

I am trying to make a codeforces crawler and I am just adding user authentication in the somehow failed to implement. Reverse not match and crawler is not a registered namespace is the error I'm getting. I don't know what files exactly are needed to put here so please ask me I will post them if you need it. I'm just a beginner and I need help.
crawler/urls.py
app_name = 'crawler'
urlpatterns = [
path('',views.index,name='index'),
path('formpage/',views.search_form_view , name='searchform'),
path('formpage/<str:handle>',views.person, name= 'person'),
path('user_login/',views.user_login,name ="user_login"),
path('logout/',views.user_logout,name="logout"),
]
base.html
<body>
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<!-- Brand -->
<a class="navbar-brand" href="{% url 'crawler:index'%}">Crawler</a>
<!-- Links -->
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="{% url 'crawler:searchform'%}">Search</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link 2</a>
</li>
{% if user.is_authenticated %}
<li class="nav-item">
<a class="nav-link" href="{%url 'crawler : logout'%}">Log Out</a>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link" href="{%url 'crawler :user_login'%}">Login</a>
{% endif %}
</li>
</li>
</ul>
</nav>
<br>
{% block body_block %}
{% endblock %}
</body>
views.py
#login_required
def user_logout(request):
logout(request)
return HttpResponse(reverse('index'))
webcrawler/urls.py
app_name = 'crawler'
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('crawler.urls',namespace= "crawler")),
]
A two-part answer here.
No reverse match at /
The point is that, in both your main and app urls.py, you registered both URLs to be '', which means that there will be a match at (empty string) but won't be one at '/'. To fix this, simply add '/' to the main urls.py as it's a better practice.
Crawler is not a registered namespace
As you call an URL, it should be {% url 'crawler:index' %} or {% url 'crawler:searchform' %} or something because crawler is the main namespace but there are multiple URLs under it so you need to pass an additional parameter after your crawler namespace.

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')),

Django context does not render in base template

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?