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

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

Related

My django admin page gives Page not found (404)

my project was running ok I used my admin page at it was all all right today I tried to open it and it gives a Page not found (404)
No Product matches the given query.
Request Method: GET
Request URL: http://127.0.0.1:8000/admin
Raised by: store.views.product_detail
No Product matches the given query.
Request Method: GET
Request URL: http://127.0.0.1:8000/admin
Raised by: store.views.product_detail
I havent touched the store app or project files at all at it was waking just fine yesterday now i cannot access admin page
project urls
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('store.urls', namespace='store')),
path('basket/', include('basket.urls', namespace='basket')),
path('account/', include('account.urls', namespace = 'account')),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
store urls
from django.urls import path
from . import views
app_name = 'store'
urlpatterns = [
path('', views.product_all, name='product_all'),
path('<slug:slug>', views.product_detail, name='product_detail'),
path('shop/<slug:category_slug>/', views.category_list, name='category_list'),
]
store views
from urllib import request
from django.shortcuts import get_object_or_404, render
from store.context_processors import categories
from .models import Category, Product
def product_all(request):
products = Product.products.all()
return render(request, 'store/home.html', {'products': products})
def category_list(request, category_slug=None):
category = get_object_or_404(Category, slug=category_slug)
products = Product.objects.filter(category=category)
return render(request, 'store/products/category.html', {'category': category, 'products': products})
def product_detail(request, slug):
product = get_object_or_404(Product, slug=slug, in_stock=True)
return render(request, 'store/products/single.html', {'product': product}) ```
context_processors.py wicht i have includet in my project settings
from .models import Category
def categories(request):
return {
'categories': Category.objects.all()
}
This is the code that causes the error
path('<slug:slug>', views.product_detail, name='product_detail'),
Change it to
path('detail/<slug:slug>/', views.product_detail, name='product_detail'),
The product_detail just overwrites the admin URL

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
)

TemplateDoesNotExist at / error in Django

I'm new to Django, and trying to create my first project. I tried to resolve the above mentioned error, spent hours banging my head against the wall, but was unable to correct it.. Given below are the relevant files. I guess it must be a small thing that I am missing, but unable to pin point it.
Views.py:
from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from datetime import timedelta
def home(request):
if request.GET.get('type') == 'formdata':
options = {'fruits': [], 'vendors': [], 'release_version': []}
try:
options['fruits'] = ['Mango', 'apple', 'banana']
options['vendors'] = ['Amazon', 'flipkart', 'myntra']
options['release_version'] = ['2015','2016','2017','2018']
today = datetime.today()
options['end_date'] = today.strftime('%Y-%m-%d')
last_week_date = today - timedelta(days=7)
options['start_date'] = last_week_date.strftime('%Y-%m-%d')
return JsonResponse({'options': options})
except:
return JsonResponse({'message': 'An error occured while getting form data'})
return render(request, 'index.html')
else:
return render(request, 'index.html')
Urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home')
]
The index.html is the template I want the urls.py to point to, and is in the templates/project_name folder in my project. I have also set the TEMPLATES_DIR in the settings.py. I wonder where I am going wrong. Any help is appreciated.

NoReverseMatch pattern not found in django2

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

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.