NoReverseMatch pattern not found in django2 - django

NoReverseMatch found for a url i have tried and
{% url '/feedback/form/' %}
i am using a feedback app.
#feedback\urls.py
from django.urls import path, include
from .views import request_feedback
urlpatterns = [
path('form/', request_feedback, name = 'get_feedback' ),
]
#feedback\views.py
def request_feedback(request):
if request.method == 'POST':
feedback_form = FeedBackForm(request.POST)
if feedback_form.is_valid():
feedback_form.save()
return redirect('')
else:
feedback_form = FeedBackForm()
return render(request, 'feedback/feedbackform.html', {'feedback_form':feedback_form})
#webapp\urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('feedback/', include("feedback.urls")),
path('user/',include("user.urls")),
]
i am getting this error
NoReverseMatch at /feedback/form/
Reverse for '' not found. '' is not a valid view function or pattern name
i am using python 3.7 and django 2
i do mitigate this problem

Related

django.core.exceptions.ImproperlyConfigured error

Here is the error message:
django.core.exceptions.ImproperlyConfigured:
The included URLconf '<module 'basicsiteApp' from '/Users/msa/trydjango/basicsite/basicsiteApp/__init__.py'>'
does not appear to have any patterns in it.
If you see valid patterns in the file then the issue is probably
caused by a circular import.
I don't have anything written in init.py because I don't know what I need to write in it so it can work.
Below is what I have in views.py:
from django.shortcuts import render
from .forms import SignUpForm
from django.contrib import messages
def signup(request):
if request.method == 'POST'
form = SignUpForm(request.POST)
if form.is_valid():
form.save()
messages.success(request, 'Account Created')
return render(request, 'signup.html')
else:
form = SignUpForm()
render(request, 'signup.html')
Basicsite/urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('basicsiteApp')),
path('admin/', admin.site.urls)
]
basicsiteApp/urls.py:
from django.urls import path
from . import views
app_name = 'basicsiteApp'
urlpatterns = [
path('', views.signup, name='signup')
]
I know it comes late, but if someone (like me) faces the same problem:
In your "Basicsite/urls.py"
path('', include('basicsiteApp')),
it should be
path('', include('basicsiteApp.urls')),
implying django to use the basicsiteApp/urls.py file instead of basicsiteApp/init.py

Why am I getting error 404 in my django project when everything seems correct?

I have two apps in my Django project:basket and store. In root url files I configured the urls.py like this:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('store.urls', namespace='store')),
path('basket/', include('basket.urls', namespace='basket')),
store/urls.py:
from django.urls import path
from . import views
app_name = 'store'
urlpatterns = [
path('', views.all_products, name='all_products'),
path('<slug:slug>/', views.product_detail, name='product_detail'),
path('category/<slug:category_slug>/',
views.category_list, name='category_list')
]
basket/urls.py:
from django.urls import path
from . import views
app_name = 'basket'
urlpatterns = [
path('', views.basket_summary, name='basket_summary'),
path('add/', views.basket_add, name='basket_add'),
]
I am getting an error:
Page not found(404)
Request Method:GET
Request URL:http://127.0.0.1:8000/basket/
Raised by:store.views.product_detail
this is my store/views.py:
from django.shortcuts import get_object_or_404, render
from .models import *
def product_detail(request, slug):
product = get_object_or_404(Product, slug=slug, in_stock=True)
context = {
'product': product,
}
return render(request, 'store/products/detail.html', context)
please help me solve this problem i've been stuck on this for a long time.
Try this way if you want to avoid 404 error:
from django.shortcuts import get_object_or_404, render
from .models import *
def product_detail(request, slug):
try:
product = Product.objects.get(slug=slug, in_stock=True)
context = {
'product': product,
}
except Product.DoesNotExist:
raise ValidationError('Product Does not exist ')
return render(request, 'store/products/detail.html', context
)

Django NoReverseMatch at http://127.0.0.1:8000/boards/1/new/

This is my site urls:
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('boards.urls'))
]
And this is my app urls:
urlpatterns = [
path('',views.home,name='home'),
path('boards/<int:pk>',views.board_topic,name='board_topic'),
path('boards/<int:pk>/new/',views.new,name='new_topic')
]
This is the Views for app:
from django.shortcuts import render,get_object_or_404
from django.http import HttpResponse,Http404
from .models import Board
# Create your views here.
def home(request):
board = Board.objects.all()
return render(request,'home.html',{
'board':board,
})
def board_topic(request,pk):
# try:
# board = Board.objects.get(pk=pk)
# except:
# raise Http404
board = get_object_or_404(Board,pk=pk)
return render(request,'topics.html',{
'board':board
})
def new(request,pk):
boards = get_object_or_404(Board, pk=pk)
return render(request, 'new_topic.html', {
'board' : boards
})
when I try to go to http://127.0.0.1:8000/boards/1/new/ then this error occurs:
enter image description here
please help me.
Here's how I was able to get rid of my error
In my app's url:
urlpatterns = [
path('',views.home,name='home'),
path('boards/<int:pk>',views.board_topic,name='board_topic'),
path('boards/<int:pk>/new/',views.new,name='new_topic')
]
In the second path I was using name= 'board_topic'
and in my templates file i was using url as href = ' url "board.pk board_topics" '
so that was why i was getting that error .
THANK YOU TO EVERYONE WHO RESPONDED

Django Url NoReverseMatch at issue

I don't know why I am getting this issue with my code. I've tried a bunch of stuff to resolve it but I can't
NoReverseMatch at /property/
Reverse for 'property_detail' not found. 'property_detail' is not a valid view function or pattern name.
Request Method:GETRequest URL:http://127.0.0.1:8000/property/Django Version:3.0.5Exception Type:NoReverseMatchException Value:
Reverse for 'property_detail' not found. 'property_detail' is not a valid view function or pattern name.
Exception Location:E:\django_projects\env\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 677Python Executable:E:\django_projects\env\Scripts\python.exePython Version:3.7.4Python Path:
['E:\\django_projects\\hotel\\src\\project',
'E:\\django_projects\\env\\Scripts\\python37.zip',
'c:\\users\\user\\appdata\\local\\programs\\python\\python37\\DLLs',
'c:\\users\\user\\appdata\\local\\programs\\python\\python37\\lib',
'c:\\users\\user\\appdata\\local\\programs\\python\\python37',
'E:\\django_projects\\env',
'E:\\django_projects\\env\\lib\\site-packages']
html code
<ul class="probootstrap-main-nav">
<li class="active">Home</li>
<li>Properties</li>
<li>Agents</li>
<li>About</li>
<li>Contact</li>
</ul>
project/urls (project directory)
urlpatterns = [
path('admin/', admin.site.urls),
path('property/', include('property.urls', namespace='property')),
path('agents/', include('agents.urls', namespace='agents')),
path('about/', include('about.urls', namespace='about')),
path('contact/', include('contact.urls', namespace='contact')),
]
propert/urls (app)
from django.urls import path
from . import views
app_name= 'property'
urlpatterns = [
path('', views.property_list, name='property_list'),
path('<int:id>', views.property_detail, name='property_detail'),
]
property/views
from django.shortcuts import render
from .models import (Property, Category)
from .forms import ReserveForm
def property_list(request):
property_list = Property.objects.all()
template = 'list.html'
context = {
'property_list': property_list,
}
return render(request, template, context)
def property_detail(request, id):
property_detail = Property.objects.get(id=id)
if request.method == 'POST':
reserve_form = ReserveForm(request.POST)
if reserve_form.is_valid():
reserve_form.save()
else:
reserve_form = ReserveForm()
return render(request, 'detail.html', {
'property_detail': property_detail,
'reserve_form': reserve_form
})

URl redirection issue in Django 2

I'm new to Django and trying to build a web app with the following structure. I need your help to understand what Im doing wrong.
The flow of the application is shadesProductUploader.urls will forward '' to authSection for login and after a successful login user should be redirected to mainSection 'home/'.
Urls.py files are
shadesProductUploader.urls
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('authSection.urls')),
]
authSection.urls
from django.contrib import admin
from django.urls import path, include
from . import views
app_name = 'authSection'
urlpatterns = [
path('', views.login_view, name='login'),
]
mainSection.urls
from django.contrib import admin
from django.urls import path,include
from . import views
app_name = 'mainSection'
urlpatterns = [
path('home/', views.home),
]
and the view.py in authSection
def login_view(request):
next = request.GET.get('next')
form = userLoginForm(request.POST or None)
if form.is_valid():
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
user = authenticate(username=username,password=password)
login(request,user)
if next:
return redirect(next)
context={'user':user}
return redirect('home/')
return render(request, 'login.html', {'form': form})
after a successful login I get this error.
What Am I missing? Not sure why I see a Url of home/ home/
Update shadesProductUploader's main section url like this:
urlpatterns = [
path('',include('mainSection.urls')),
... # other urls
]
And change in mainSection urls like this:
urlpatterns = [
path('home/', views.home, name="home"), # <-- added name here
]
And in view, use it like this:
if next:
return redirect(next)
context={'user':user}
return redirect(reverse('home'))
Here, we have named our url as home. And we got the url using reverse.