TemplateDoesNotExist at / error in Django - 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.

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

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

Why do I get circular import error when importing view?

Could someone please help me. I get the following error when loading my project:
raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
django.core.exceptions.ImproperlyConfigured: The included URLconf
'siteV1.urls' 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.
This is my urls.py
from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from drink.views import HomepageView, CreateDrinkView, CreateNormalDrinkView, CreateMixedDrinkView
from machine.views import SettingsView
from account.views import LoginView
urlpatterns = [
path('', HomepageView.as_view()),
path('create-drink', CreateDrinkView.as_view()),
path('create-drink/save-normal', CreateNormalDrinkView.as_view()),
path('create-drink/save-mixed', CreateMixedDrinkView.as_view()),
path('settings/', SettingsView.as_view()),
path('accounts/login/', LoginView.as_view()),
path('admin/', admin.site.urls),
]
if settings.DEBUG == True:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
else:
pass
#enter code here for media and static handeling during production
The problem seems to be related to the import of the LoginView. If I delete this import and path the program runs without any errors. My accounts.views contains the following code:
from django.contrib.auth import authenticate, login
from django.shortcuts import render, redirect
from django.views.generic import TemplateView
# Create your views here.
class LoginView(TemplateView):
template_name = 'login.html'
def get(self, request):
return render(request, self.template_name, {})
def post(self, request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
# Redirect to a success page.
return redirect('')
else:
# Return an 'invalid login' error message.
return render(request, self.template_name {'error': 'Gebruikersnaam of wachtwoord onjuist!'})
The account.models is empty at the moment. I tried running pycycle to check for circular imports. Pycycle returned that there were no circular imports found. Your help is much appreciated!
Solved this by deleting the app and recreating it. Still don't know what the problem is but it works for now.