how can i create a html file template in django application? - django

I'm learning Django and I have a problem that there is no answer for it on internet
here is the problem,
I created a folder as 'app', and I create another folder in app as 'challenges' (in VS Code in a Django application),
so we have 2 nested folders
now I want to create an HTML page, and I create a file as 'index.html',
but VS Code cannot know this file as an HTML file

You need to make a folder structure like this
>app_name
>>...
>>templates
>>>app_name
>>>>index.html
views.py
from django.shortcuts import render
def index(request):
return render(request, 'app_name/index.html)
urls.py
from . import views
urlpatterns = [
path('', views.index),
]

Related

Change URL for wagtail blog index page

I currently have a website where the home page at www.mysite.com is the wagtail blog index page
I wish to move the blogindex page to another url
I can easily have a different homepage by amending my urls.py file:
#original
path("", include(wagtail_urls))
#new
path(
"",
TemplateView.as_view(template_name="pages/newhomepage.html"),
name="newhomepage",
),
However I would like the blogindex page available at e.g. myste.com/blog but I am not sure how to go about this. Adding the following to urls.py does not do it
path("blog/", include(wagtail_urls))
There are a couple of changes in project required to achieve this.
Firstly, create a urls.py file inside your specific app, this will be different from the one that you should already have alongside wsgi.py, asgi.py, etc. and it will only store app specific urls.
from django.urls import path
from .views import index
urlpatterns = [
path('test/', index, name='generalized-test-url'),
]
Now, you should render the template inside your view.
def index(request):
return render(request, 'pages/newhomepage.html', context)
Lastly, in your root urls.py file, you need to include the app urls. Lets say your app name is blog.
from django.urls import path, include
urlpatterns = [
path('blog/', include('blog.urls')),
]

Rendering new custom html templates in existing Django project

I'm not a Django expert but I need to make customizations of this software which is written in Django using the rest framework.
I need to render a custom html template let's say a.html, however when I go to http://localhost:8080/custom/custom/a I get redirected back to different page - not a.html.
This is what I've done so far. I created a new folder in apps called custom:
cvat
apps
custom
templates
custom
_init.py
a.html, b.html, ...
urls.py
apps.py
from django.apps import AppConfig
class CustomConfig(AppConfig):
name = 'cvat.apps.custom'
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('custom/<str:name>', views.CustomView),
]
views.py
from django.shortcuts import render
def CustomView(request, name):
return render(request, 'custom/'+name+'.html')
In addition, I have modified these existing files to add the custom folder that was created in apps:
in urls.py added my url patterns:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('cvat.apps.engine.urls')),
path('django-rq/', include('django_rq.urls')),
path('custom/', include('cvat.apps.custom.urls')),
]
in base.py added this:
INSTALLED_APPS = [
...
'cvat.apps.custom'
]
Any advice on what I'm doing wrong?
You can try something like this
url can be like this
urlpatterns = [
path('custom/<name>/', views.CustomView),
]
and your view should be like this.
def CustomView(request, name):
return render(request, f'custom/{name}.html')
Hope this will help you.

django project always trying (and failing) to load file in separate app

I'm going back to an old django project and am trying to make a new site in a new app I just made, popularify. I want to load an httpResponse and nothing else when I go to the /popularify path , but it keeps trying and failing to get a favicon.ico file, the 404 error always appears in the chrome web console and django logs.
I have it set up correctly so my popularify page loads a basic httpResponse:
my main urls.py file in my project folder has a path:
path('popularify/', include('popularify.urls')),
The popularify app urls.py file looks like this:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
And the popularify views.py file looks like this
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
All very basic to just load some text, but in console I am getting a unrelated 404 error trying to load a file I never directly specified, and dont want to load:
If I click the favicon.ico link it shows the html file _base.html I have in my templates folder, a file I use as a base file in my pages/ app, which is completely separate and something I don't want to load in my new popularify app. Do I need to tell my popularify path to specifically not use this _base.html file in my templates folder?

Django Caching on front-end

I was working with 2 applications that are within a DJango project: "customer" and "vendors". Each application has a HTML file named "testindex.html".
Whenever I typed:
http://myhost/customer/basic_info
the correct page would show up
If I typed
http://myhost/vendors/basic_info
the page from http://myhost/customer/basic_info would show up
I found out that it was due to caching (since both applications use "testindex.html"). So again, "testindex.html" is caching.
How can one get around this problem?
TIA
Details are listed below. I have the following views defined:
urls.py for the project
urlpatterns = [
... snip ...
url(r'^customer/', include('libmstr.customer.urls')),
url(r'^vendors/', include('libmstr.vendors.urls')),
]
views.py for customer
from django.shortcuts import render
def basic_info(request):
return render(request, 'testindex.html', {})
views.py for vendors
from django.shortcuts import render
def basic_info(request):
return render(request, 'testindex.html', {})
urls.py for customers
from django.conf.urls import url
from . import views
# list of templates
app_name = 'customer'
urlpatterns = [
url(r'^basic_info/$', views.basic_info, name='basic_info'),
]
urls.py for vendors
from django.conf.urls import url
from . import views
# list of templates
app_name = 'vendors'
urlpatterns = [
url(r'^basic_info/$', views.basic_info, name='basic_info'),
]
It sounds like you have two templates, customers/templates/testindex.html and vendors/templates/testindex.html.
When you call render(request, 'testindex.html', {}), the app directories template loader searches the templates directory for each app in INSTALLED_APPS, and stops the first time it finds a match. If customers is above vendors in INSTALLED_APPS, then it will always use the customers template.
For this reason, Django recommends that you name your templates customers/templates/customers/testindex.html and vendors/templates/vendors/testindex.html, and change your views to use customers/testindex.html and vendors/testindex.html. This way you avoid clashes.

Django views in project directory

I have a project with two apps with their respective views, but I want to create a views.py in django project directory for some generic pages such as about, login... It is correct to place the views.py and templates in the root project folder for this purpose?
I have been reading about this. The practice more recommended for this is create an app that creates cohesion between other apps, call it web_site or site whatever is better for you.
python manage.py startapp my_site
Everything in this site app will be done the regular way.
In the projects url you will want to import the url in this way so the web pages appear in the / url pattern.
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('my_site.urls'))
]
I do this for project level pages like you describe. These are usually simple pages that tie the project's apps together, and not contain any business logic.
You can do that by making views.py file into the project directory.
from django.contrib import admin
from django.urls import path, include
from attendance import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('insert.urls')),
path('login/', views.logins, name='login'),# relevant line
]
And this is the code of login view
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def logins(request):
return HttpResponse("THIS IS LOGIN PAGE !!!")