Django load wrong template - python

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.

Related

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.

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

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".

ImportError: No module named app.views django 1.10 python

I just have started learning django in its 1.10 version. In a project (called refugio) I have created an app called mascota.
This is my views.py file for my app:
from __future__ import unicode_literals, absolute_import
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("Index")
Also, I already have written my urls.py file for it:
from django.conf.urls import url
from apps.mascota.views import index
urlpatterns = [
url(r'^$/', index),
]
And I have modified the url.py file of my project:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('apps.mascota.urls')),
]
But when I run the server of my project, it sends me the next error message:
If it helps, My dir tree is the following:
REFUGIO
apps
mascota
views.py
urls.py
refugio
settings.py
urls.py
manage.py
I know this is a dummy question, but I don't know what is wrong, I have already checked my urls sentences, but I see everything ok.
I will appreciate your help.
Ps.: My app is located inside a folder called apps.
Regards.
Every Python package need __init__.py file (read this).
REFUGIO
apps
mascota
__init__.py
views.py
urls.py
__init__.py
refugio
__init__.py
settings.py
urls.py
manage.py
Change your file structure into
REFUGIO
mascota
views.py
urls.py
refugio
settings.py
urls.py
manage.py
and then change the line in urls.py
from apps.mascota.views import index
into
form mascota.views import index
Hope this helps.

How to setup urls.py to work with app/urls.py and templates in src with Django

Trying to figure out how to setup my own project.
I created a new Django app to make a homepage.
src/home/urls.py:
from django.conf.urls import url
urlpatterns = [
url(r'^$', 'views.index', name='index'),
]
src/home/views.py:
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request, "index.html", {})
src/project/urls.py:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^', include('home.urls')),
url(r'^admin/', admin.site.urls),
]
src/templates/index.html:
<h1>Hello World</h1>
The reason this isn't in a templates folder inside of the home app is because I want to use this as my base template eventually for all apps and pages
Error reads:
ImportError at /
No module named 'views'
Using python 3.5 and django 1.9
EDIT*** changed to home.views.index
ERROR now reads:
TemplateDoesNotExist at /
index.html
In Site-wide Urls.py do the Following:
from app_name import urls as app_name_urls
from django.conf.urls import include
urlpatterns=[
path('',include(app_name_urls)
]
In App/urls.py
from django.urls import path
from .import views
urlpatterns=[
#your paths go here
]
Make sure you home is a package and you have __init__.py there.
You might also need to change views.index to home.views.index in your urls.py

Categories