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

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.

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 %}

Active nav-item highlighting based on pk passed in url

I have 4 nav-items being generating dynamically based on the Django Model entries.
My template code is as below:
{% for stockbroker in stockbrokers %}
<li class="nav-item">
<a class="nav-link" href="{% url 'broker:display_stocks' stockbroker.id %}" id="nav">{{ stockbroker.broker_name }}
</a>
</li>
{% endfor %}
I want to highlight the currently active nav based on the id I am passing in the url section of the a tag href. Is it possible to achieve this?
I am generating these nav links in the base.html using context_processors.py
from .models import StockBroker
def stockbrokers(request):
return {'stockbrokers': StockBroker.objects.all()}
An if condition should work for this:
{% for stockbroker in stockbrokers %}
<li class="nav-item">
<a class="nav-link {% if stockbroker.id == current_id %}active{% endif %}" href="{% url 'broker:display_stocks' stockbroker.id %}" id="nav">{{ stockbroker.broker_name }}
</a>
</li>
{% endfor %}
Note: current_id (or variable name of your preference) should be passed in the context.
The context is the dictionary that is passed while rendering the template, example:
def my_view(request):
# View Code
return render(request, 'template_name.html', {'current_id': current_id})

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.

Active Tag in for extended templates

I'm a newbie to Django and want some help on template inheritance.
I want to set "class="active" for the current active page but how do I do with template inheritance,as it generalizes it for all the pages but the active should change on changing current active page.
I know this question might be silly but I don't know the answer still.
I have no idea what to do for this case.
Extended html
<ul class="navbar-nav mr-auto">
<li class="nav-item mx-3">
<a class="nav-link" {% if '/Homepage/' in request.path %} class ="active" {% endif %} href="{% url 'success' %}">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item mx-3">
<a class="nav-link" {% if '/booking/' in request.path %} class ="active" {% endif %} href="{% url 'booking' %}">Make a Booking</a>
</li>
<li class="nav-item mx-3">
<a class="nav-link" {% if '/Hsitory/' in request.path %} class ="active" {% endif %} href="{% url 'History' %}">History</a>
</li>
<li class="nav-item mx-3">
<a class="nav-link" {% if '/pending/' in request.get_full_path %} class ="active" {% endif %} href="{% url 'pending' %}">Pending requests</a>
</li>
</ul>
urls.py
urlpatterns = [
path('', views.loginpage, name="loginpage"),
path("failed/", views.failed, name="failed"),
path("Homepage/", views.success, name="success"),
path("loggedout/", views.loggedout, name="logout"),
path("booking/", views.booking, name="booking"),
path("pending/", views.pending, name="pending"),
path("History/", views.History, name="History"),
path("Register/", views.Register, name="Register"),
]
I expect the active class might get applied to only current active pages.
You can try like this.
<a class="nav-link" {% if '/Homepage/' in request.path %} class ="active" {% endif %} href="/Homepage/">Home <span class="sr-only">(current)</span></a>
Also better use the django url template tag
EDIT: I think you are providing wrong url patterns
You can also do this
{% url 'homepage' as url %}
<a {% if request.path == url %} class="active"{% endif %} href="{{ url }}" >Homepage</a>
You have two class attribute in your anchor tag so you can change this to
<a class="nav-link{% if request.path == url %} active{% endif %}">Home <span class="sr-only">(current)</span></a>
Extended Html
<li class="nav-item mx-3">
{% url 'success' as Homepage %}
{% if request.path != Homepage %} <a class="nav-link" href="{% url 'success' %}">Home <span
class="sr-only">(current)</span></a>
{% else %}
<a class="nav-link active" href="{{ Homepage }}">Home <span class="sr-only">(current)</span></a>
{% endif %}
</li>
<li class="nav-item mx-3">
{% url 'booking' as booking %}
{% if request.path != booking %} <a class="nav-link" href="{{ booking }}">Make a Booking</a>
{% else %}
<a class="nav-link active" href="{{ booking }}">Make a Booking</a>
{% endif %}
</li>
<li class="nav-item mx-3">
{% url 'History' as History %}
{% if request.path != History %} <a class="nav-link" href="{{ History }}">History</a>
{% else %}
<a class="nav-link active" href="{{ History }}">History</a>
{% endif %}
</li>
<li class="nav-item mx-3">
{% url 'pending' as pending %}
{% if request.path != pending %} <a class="nav-link" href="{{ pending }}">Pending requests</a>
</li>
{% else %}
<a class="nav-link active " href="{{ pending }}">Pending requests</a>
{% endif %}
</li>
Used request.path for the solution