Django URLConf: page not found error - python

I am getting the error when resolving url http://127.0.0.1:8000/userprofile/auth. Below is my urls.py file for the userprofile app:
from django.conf.urls import patterns, url
from userprofile import views
urlpatterns = patterns('',
url(r'^$', views.login),
url(r'^auth/$' , views.auth_view),
url(r'^logout/$', views.logout),
url(r'/loggedin/$', views.loggedin),
url(r'/invalid/$', views.invalid_login),
)
The main urls.py is as follows:
from django.conf.urls import patterns, include, url
from userprofile import views
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^userprofile/', include('userprofile.urls')),
url(r'^admin/', include(admin.site.urls)),
)
Here's a link to the screenshot of browser.

You should add the / slash to the action attribute of your <form>:
<form action="/userprofile/auth/" method="POST">
Or, as the better solution, name the url:
url(r'^auth/$' , views.auth_view, name='auth_view'),
and use the {% url %} tag in the template:
<form action="{% url 'auth_view' %}" method="POST">

Related

Reverse for 'AddCart' not found. 'AddCart' is not a valid view function or pattern name

I am Working on my project and I don't know how this error I got
Can anybody see what I'm missing?
This is my root project urls.py
from django.contrib import admin
from django.urls import path, include
from .import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index),
path('onlinePizza/', include('onlinePizza.urls')),
path('cart/', include(('cart.urls'), namespace='cart')),
path('accounts/', include(('accounts.urls'), namespace='accounts')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This is my cart app urls.py
from django.urls import path
from .import views
from cart.views import AddCart
app_name = 'cart'
urlpatterns = [
path('add_cart/', views.AddCart, name='Cart'),
]
This is my cart app views.py
from django.shortcuts import render, redirect
from cart.models import *
def AddCart(request):
product = request.POST.get('product')
remove = request.POST.get('remove')
cart = request.session.get('cart')
if not cart:
request.session['cart'] = {}
if cart:
quantity=cart.get(product)
if quantity:
if remove:
if quantity<=1:
cart.pop(product)
else:
cart[product]=quantity-1
else:
cart[product]=quantity+1
else:
cart[product]=1
else:
cart={}
cart[product]=1
request.session['cart']=cart
return redirect ('Menu')
This is my onlinePizza/pc_menu.html template
<form action="{% url 'cart:AddCart' %}" method="POST">{% csrf_token %}
<input hidden type="text" name="product" value="{{i.id}}">
<button class="main-btn cart cart-btn" style="padding: 5px 32px">Add <i class="fa-solid fa-cart-shopping"></i></button>
</form>
I try to solve this problem, but I show this error everywhere in my project.
You need that name:
urlpatterns = [
path('add_cart/', views.AddCart, name='Cart'),
]
and that link generator:
{% url 'cart:AddCart' %}
refering to the exactly same value. So it has to be either 'Cart' and 'cart:Cart' or 'AddCart' and 'cart:AddCart'.

How to fix 'str' object is not callable in django

Hi Everyone I made A Blog but when i delete post i get this error i'm using django latest version:
TypeError at /post/(?P3\d+)/remove/
'str' object is not callable'
Here Is My Views.py
class PostDeleteView(LoginRequiredMixin,DeleteView):
success_url = reverse_lazy('post_list')
model = Post
Here is my app Urls.py
from django.urls import path
from blog import views
urlpatterns = [
path('',views.PostListView.as_view(),name='post_list'),
path('about/',views.AboutView.as_view(),name='about'),
path('register/',views.user_register,name='user_register'),
path('post/(?P<pk>\d+)',views.PostDetailView.as_view(),name='post_detail'),
path('post/new/',views.CreatePostView.as_view(),name='post_new'),
path('post/(?P<pk>\d+)/edit/',views.PostUpdateView.as_view(),name='post_edit'),
path('post/<int:pk>/remove/',views.PostDeleteView.as_view(),name='post_remove'),
path('drafts/',views.PostDraftListView.as_view(),name='post_draft_list'),
path('post/(?P<pk>\d+)/comment/',views.add_comment_to_post,name='add_comment_to_post'),
path('comment/(?P<pk>\d+)/approve/',views.comment_approve,name='comment_approve'),
path('comment/(?P<pk>\d+)/remove/',views.comment_remove,name='comment_remove'),
path('post/(?P<pk>\d+)/publish/',views.post_publish,name='post_publish'),
]
Image
https://i.stack.imgur.com/wbcfn.png
Here is my post_confirm_delete.html
{% extends "blog/base.html" %}
{% block content %}
<form method="POST">
{% csrf_token %}
<p>Are You sure you want to delete {{ object }}?</p>
<input type="submit" class="btn btn-danger" value="Confirm">
</form>
{% endblock %}
Here is my Project Urls.py
"""blogpro URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path,include
from django.contrib.auth.views import LoginView,LogoutView
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')),
path('accounts/login/', LoginView.as_view(), name='login'),
path('accounts/logout/', LogoutView.as_view(), name='logout', kwargs={'next_page': '/'}),
]
You are using path with regex which is not how it work docs, only use regex url string with re_path
it should be :
path('post/<int:pk>/remove/',views.PostDeleteView.as_view(),name='post_remove'),

NoReverseMatch - not a registered namespace

I am new to Django and I was working at my first project, but I got:
NoReverseMatch with Exception Value: 'hiring_log_app' is not a
registered namespace in base.html
which follows:
href="{% url 'hiring_log_app:index' %}" >Hiring Log
href="{% url 'hiring_log_app:topics' %}" >Topics
I made sure my namespace was correct and I looked at the other topics already opened without figuring out how to solve the issue.
I paste urls.py:
from django.contrib import admin
from django.conf.urls import include, url
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'', include ('hiring_log_app.urls', namespace='hiring_log_app')),
hiring_log_app/urls.py:
"""Define URL patterns for hiring_log_app"""
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^topics/$', views.topics, name='topics'),
]
views.py:
from django.shortcuts import render
from .models import Topic
def index(request):
"""The home page for hiring_log_app"""
return render(request, 'hiring_log_app/index.html')
def topics(request):
""" list of topics"""
topics = Topic.objects.raw( "SELECT text FROM HIRING_LOG_APP_TOPIC;")
context = {'topics' : topics}
return render(request, 'hiring_log_app/topics.html', context)
Does anyone know where I am making a mistake?
You have wrongly specified the namespace in urlpatterns. Follow the pattern below:
from django.contrib import admin
from django.conf.urls import include, url
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'', include (('hiring_log_app.urls','hiring_log_app'), namespace='hiring_log_app'))
Edit:
If you are using Django 2.2.4 then you should use path instead of url since the usage of it is replaced by re_path.
from django.contrib import admin
from django.conf.urls import include
from django.urls import re_path
urlpatterns = [
re_path(r'^admin/', include(admin.site.urls)),
re_path(r'', include (('hiring_log_app.urls','hiring_log_app'), namespace='hiring_log_app'))

Django - urls.py in project, and url.py in app

I'm trying to get a form action redirect to another page, however, I'm not too farmiliar with the Django framework. I went through the polls tutorial.
The current urls.py in my project is:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from WebApp import views
from StripCal import views
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^webapp/', include('WebApp.urls', namespace="WebApp")),
url(r'^stripcal/', include('StripCal.urls', namespace="StripCal")),
)
The url.py in my stripcal app is:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from StripCal import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^run', views.detail, name='detail'),
)
When I type in
http://127.0.0.1:8000/stripcal/
http://127.0.0.1:8000/webapp/
It successfully goes to two different apps. However, I'm not too familiar with {% url 'app_name:view_name' %} syntax. It seems like 'app_name:view_name' becomes /app_name/view_name
This is my current view:
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views import generic
# Create your views here.
def index(request):
context = {'somethingDownByCelery': "heh"}
return render(request, 'StripCal/index.html', context)
def detail(request):
context = {'somethingDownByCelery': "heh"}
return render(request, 'StripCal/detail.html', context)
My Index.html
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'StripCal/index.css' %}"/>
<form action="{% url 'stripcal:detail'%}" method="post">
{% csrf_token %}
<p> StripCal Input </p>
<textarea name="StripCal_Input" cols="30" rows="10"> </textarea>
<br> <br>
<input type="submit" value="Submit">
</form>
My Detail.html
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'StripCal/detail.css' %}"/>
Hello Detail!
When I remove action={% url 'stripcal:detail' %} the webpage loads, however, when I put it in, the page does not even load (HTTP 500).
try to change:
StripCal.url.py
urlpatterns = patterns('StripCal.views',
url(r'^$', 'index', name='index'),
url(r'^run','detail', name='detail'),
)

Django NoReverseMatch error, reverse function not work with no arguments

this NoReverseMatch error is driving me nuts. I'm using Django 1.6 and I have checked through my urls and it just doesn't work. Please kindly guide me on this.
I basically want to do something deadly simple, just when I submit a html form, I get the data I want and then redirect to a result page, but it just doesn't work...
Here is my index.html file
<form name="input" action="{% url 'whatspring:sending' %}" method="post">
{% csrf_token %}
Recipient: <input type="text" name="usrname">
<br>
<input type="submit">
</form>
<br>
my view.py
def index(request):
return render(request,'springsend/index.html')
def sending(request):
var = request.POST['usrname']
doSomethinghere()
return HttpResponseRedirect(reverse('whatspring:results'))
def results(request):
return render(request,'springsend/send_results.html')
then my app urls.py
from django.conf.urls import patterns, url
from springsend import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^results/$', views.results, name='results'),
)
and the main urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^send/', include('springsend.urls', namespace="whatspring")),
)
I have tried to look into the problem and it seems that the reverse function cannot get the name for the reverse url that I want (i.e. 'results' under the namespace 'whatspring'....)Am I missing something something trival? Please kindly help.
Your urls.py (springsend one) doesn't seem to have a url for the sending view, that's probably why {% url 'whatspring:sending' %} can't find it.
Simply change it to
from django.conf.urls import patterns, url
from springsend import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^results/$', views.results, name='results'),
url(r'^sending/$', views.sending, name='sending'), # this line
)
Every accessible view needs a url. The user's browser needs to have some address to send things. If it would just send it to your domain without url, Django would have no way to tell which url is requested. Django does not auto-generate these urls (which is probably good).
(The user himself does not need to know this url; you don't need to place any ` links anywhere.)

Categories

Resources