GET http://localhost:8000/add/ Page not found - python

I've been working on this application using my localhost:8000 for a while now and everything had been working smoothly. However now that I have tried to add a new url: /add/. For some reason it doesn't recognise the URL. I believe maybe there's something wrong with how I've wrote my code but I haven't quite found it. Any help would be great!
To provide context I started of in my urls.py file where I created a new path: path('add', views.ProjectCreateView.as_view(), name='add'),
I then moved over to my views.py file and imported the CreateView as so:
from django.views.generic import CreateView.
From there I then created a class for the view:
class ProjectCreateView(CreateView):
model = Project
template_name = 'budget/add-project.html'
fields = ('name', 'budget')
Following this I then created another file within my budget folder nested in my templates folder. The file name add-project.html. I don't think there's anything wrong with this file but just to guarantee this is how I linked my html file:
{% extends 'budget/base.html' %}
{% load static %}
{% block content %}
This is the exact message I get when I run http://localhost:8000/add/
"No Project matches the given query."
ULRS.PY in main working folder:
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('', views.project_list, name='list'),
path('add', views.ProjectCreateView.as_view(), name='add'),
path('<slug:project_slug>/', views.project_detail, name='detail')
]
URLS.PY in subfolder:
from django.urls import path, include
from django.contrib import admin
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('budget.urls'))
]

When you request /add/ with the trailing slash, this does not match the add path but does match the <slug:project_slug>/ path, so it tries your project_detail view with project_slug set to "add".

Related

Django load wrong template

I am new to Django. I am trying to configure an app called 'faq', and an index page to display the different "apps", including the previous one.
I was following the Learn Django site guide to decide what was best for the template structure.
So, I configured myproject/settings.py to let load the template from templates folder: 'DIRS': ['templates'],.
In the app folder myproject/apps/faq I have created a templates folder.
In the app views myproject/apps/faq/views.py I defined the views to load the data from database and send it back to the template:
from django.shortcuts import render
from .models import *
def faqs_view(request):
faqs = FrequentAskedQuestion.objects.all()
context = { 'faqs': faqs }
return render(request, 'index.html', context)
def faq_view(request, id):
faq = FrequentAskedQuestion.objects.get(id=id)
print(faq)
context = { 'faq': faq }
return render(request, 'detail.html', context)
In the app folder I have created a URLconf configuration in myproject/apps/faq/urls.py:
from django.urls import path
from .views import faqs_view, faq_view
urlpatterns = [
path('', faqs_view, name='faq-list'),
path('<int:id>', faq_view, name='faq-detail'),
]
A parenthesis here, since the first attempt I have tried to create a project templates folder and use the project views.py and urls.pyand it didn't work I decided to create another app called main.
In the app folder myproject/apps/main I have created a templates folder.
In the app views myproject/apps/main/views.py I defined just a simple view to render the app list:
from django.shortcuts import render
def index_view(request):
return render(request, 'index.html')
In the app folder I have created also a urls.py and added the previous view:
from django.urls import path
from .views import index_view
urlpatterns = [
path('', index_view, name='site-index'),
]
In the project URLconf file I have added both apps:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('myproject.apps.main.urls'), name='site-index'),
path('admin/', admin.site.urls, name='admin-index'),
path('faq/', include('myproject.apps.faq.urls'), name='faq-index'),
]
After the previous setup I got working 'faq' app and it works great. But, whenever I go to http://localhost:8000/ it display the title from myproject/apps/faq/templates/index when it should display myproject/apps/main/templates/index.
Maybe it is worth to mention that both templates (main/templates/index and faq/templates/index) inherit from a base template that is created in each app template folder.
The file tree:
myproject
apps
faq
templates
base.html
index.html
urls.py
views.py
main
templates
base.html
index.html
urls.py
views.py
settings.py
urls.py
I am doing something wrong. So, any clues?
Typically you write the name of the app under the templates directory, so:
faq/
templates/
faq/
index.html
This creates a sort of "namespace", such that you can reference to the correct file with:
from django.shortcuts import render
def index_view(request):
return render(request, 'faq/index.html')
If you do not construct such directory, then Djangno will simply search for a file named index.html in all the template directories. So if there are multiple, it depends on the order in which the template directories are searched.

How to Change Django Default Page to your Design?

I uploaded my site to the live server, but still, I am seeing Django default page, I updated my urls.py file, but still, it's not working. and it's showing me this output. Please let me know where I am Mistaking.
I am trying to access this mydomain.com/dashboard
Using the URLconf defined in yoursite.urls, Django tried these URL patterns, in this order:
admin/
The current path, dashboard, didn't match any of these.
here is my urls.py file...
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^dashboard/', include('dashboard.urls')),
url(r'^accounts/', include('accounts.urls')),
url('', include('frontpanel.urls')),
path('', views.index, name='index'),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
And here is my views.py file...
def index(request):
category = Category.objects.all().order_by('created_at')
return render(request, "base.html", {'category':category})
What version of Django are you using?
Try changing url to re_path (remember to import it first). I think url has been deprecated.
After Analyzing the question u never mentioned ur app urls.py you only mentioned project urls.py . As u have url(r'^dashboard/', include('dashboard.urls')), in ur project urls.py, there must be a file in your app also named urls.py which handles urls having prefix /dashboard/ , by default django doesnt make that file you need to manually make it add ur function names to redirect . For example this is my main urls.py file
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('display_report/', include("display_report.urls"))
]
then u have to make a file named urls.py in ur app also to handle the requests and redirect the the approaite functions , in my display_report app i made a urls.py that looks like this
from django.contrib import admin
from django.urls import path, include
from display_report import views
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [path('', views.index)]
And then it will redirect to function named index in ur views.py file inside ur app
from django.shortcuts import render, redirect, HttpResponse
# from .models import Employee, Tasks, Report, Fileupload, Fileuploadnext
from django.views.decorators.csrf import csrf_exempt
# Create your views here.
def index(request):
return render(request."index.html")
Here my ur will be mydomain.com/display_report and my index.html file will be inside the template folder

Django templates won't load but HttpResponse will

I recently began learning django and have been running into some trouble and I can't figure out why.
My problem is quite simple: whenever I try to run my homepage with a template, it throws up a 404 error.
My file hierarchy is as such:
crm
accounts
templates
accounts
dashboard.html
urls.py
views.py
crm
urls.py
In crm/urls, I have
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('accounts.urls'))
Then in accounts/urls, there is,
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
]
And in views, I have
from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return render(request, 'accounts/dashboard.html')
My dashboard.html is just a basic html file with a title and h1, that's it.
Also, whenever I change
return render(request, 'accounts/dashboard.html')
to
return HttpResponse('home')
in my home function, it decides to show.
Your dashboard.html is inside a folder, so it should be referenced like accounts/dashboard.html
Relevant docs: https://docs.djangoproject.com/en/3.0/intro/tutorial03/#a-shortcut-render
In their example, they are using polls/index.html (scroll a bit up from the linked position in the docs). It is analogous to your accounts/dashboard.html in placement (both are in a subfolder as they should be).
Django looks for templates across multiple apps in the same way it searches for static files. Except that you can "see" static files being copied over to a separate folder with manage.py collectstatic while templates get "discovered" in place and not copied anywhere. In facg, if you are running manage.py runserver then the static files are also discovered in place, so the logic is the same.
One difference is that template loading can be changed by using a different set of loaders (docs on template loaders, but by default (and for the majority of projects I guess) this similarity holds.

When I try to write something in Django urls.py file, it doesn't show this in the browser

#code in urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name= 'index'),
path('about/', views.about, name ='about'),
]
#code in views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello")
def about(request):
return HttpResponse("about harry")
Body
I am doing this while watching a tutorial, and the same thing is working for him. Moreover, if mistakenly I write the wrong code and run, cmd shows the errors and on reloading the browser the server doesn't work more. Then I need to restart CMD and again run manage.py. Please tell me the reason and also the solution, Thanks.
What errors do you get?
Besides there're 2 urls' files: url routes for pages are inside urls.py in the same folder where views.py are. In another urls.py (the one which is in the same folder with manage.py) you may need to write following, assuming that 'polls' is the name of you application:
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
]
According to Django docs: include() function allows referencing other URLconfs. Whenever Django encounters include(), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.
You should always use include() when you include other URL patterns. admin.site.urls is the only exception to this.
If you still get the error: check that you’re going to http://localhost:8000/polls/ and not http://localhost:8000/

Error: Template Does Not Exist

I have two apps in one project the main apps call pages contain Home and About page the second apps is contact page.
Here is my contact/urls.py in contact apps:
from .views import ContactFormView
urlpatterns = patterns('',
# URL pattern for the ContactView
url(regex=r'^contact/$', view=ContactFormView.as_view(), name='contact'),
)
the main apps pages/urls.py :
from django.conf.urls import patterns, url
from .views import AboutPageView, HomePageView
urlpatterns = patterns('',
url(regex=r'^$', view=HomePageView.as_view(), name='home'),
url(regex=r'^about/$', view=AboutPageView.as_view(), name='about'),
)
in main apps pages/views.py :
from vanilla import TemplateView
class HomePageView(TemplateView):
template_name = "pages/home.jade"
class AboutPageView(TemplateView):
template_name = "pages/about.jade"
class ContactFormView(TemplateView):
template_name = "contact/email_form.jade"
I get this error say django.template.base.TemplateDoesNotExist
TemplateDoesNotExist: /contact/email_form.jade
I am not sure why, the setting file is OK I can open the main page with the contact page in the menu nav when I click contact I get the error above. Any helps appreciate
Thanks
You should have the email_form.jade file inside /templates/contact. Or place it in /templates/pages and change the line to template_name = "pages/email_form.jade".
Check to make sure you don't have the any of the parent directories the template is in marked as static in app.yaml.
App Engine stores and serves static files separately from application files. Static files are not available in the application's file system.
If you do either find a separate directory to place static files or you can optionally use
application_readable: True in the configuration block for static files which allows the filesystem to read them.
See here for more details

Categories