How to call a template? - django

How can I call the the about.html from the following index.html? What to add to urls and views?
All the html files including about and index are collected in the project/static folder.
# This is part of the index.html, where I want to call the about.html
<div class="card-footer">
Learn More
</div>
# Here is the project/urls.py
from django.contrib import admin
from django.urls import path
from app1 import views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index),
]
urlpatterns += staticfiles_urlpatterns()

If you are in one html file and want to open another html file you can put something like this in your html file:
Go to About
This will create a clickable link on the words Go to About. You can then position this anywhere you want on your page. In your urlpatterns you would need to make a new path like this
path('about', view.AboutView.as_view(), name='about')
Within your views.py you have to make a class called AboutView using templateView and put the name of your html file as template_name like this:
from django.views.generic.base import *
class AboutView(TemplateView):
template_name = "about.html"
Let me know if you need more help understanding this code or want to do something else

add about url in your urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index),
path('about/', views.about, name="about"),
]
add about views in your views.py file
def about(request):
# render your template here
in index.html
<div class="card-footer">
Learn More
</div>

Related

url getting doubled when opening a link from the home page.(django)

On clicking the 2nd item in the list it opens the events.html page but the url shows:
http://127.0.0.1:8000/events/events/ instead of http://127.0.0.1:8000/events
Home page code:
<li>HOME</li>
<li>EVENTS</li>
<li>REGISTERED TEAMS</li>
<li>CONTACT US</li>
Urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home_page, name='home_page'),
path('hackathon_register/',views.hackathon_register,name='hackathon_register'),
path('events/',views.events,name='events')
]
views.py
def events(request):
return render(request,'testapp/events.html',{})
From here, it seems that your code in urls.py of project is:
.......
urlpatterns = [
....
path('events/', include("app.urls")),
]
So correct this mistake either by changing project.urls or app.urls.

django path showing twice in url bar

I'm using django to make a little weather app for uni and I'm trying to link from the index page to a 'detail' page but in the url the path keeps apearing twice...
I've tried rewriting out the entire urls.py page (both of them). I've looked online as well but there dosent seem to be much about this (or I'm not looking in the right places)
so when I click on 'more info' for day 1 the url bar shows /day1/day1 instead of just /day1
[urls.py]
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('weather.urls')),
path('day2/', include('weather.urls')),
path('day1/', include('weather.urls')),
]
[urls.py 2]
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('day1/', views.day1, name='day1'),
path('day2/', views.day2, name='day2'),
]
[views.py]
def day1(request):
return render(request, 'weather/day1.html')
def day2(request):
return render(request, 'weather/day2.html')
[link tag in main html page]
day 1

How to navigate from current html page to another html page in django

I'm working on django app and i'm facing a problem when i need to navigate from my index.html page to another about.html page. I'm setting everything in this way below:
urls.py (myproject)
from django.urls import path, include
urlpatterns = [
path('', include('weather_app.urls')),
path('about/', include('weather_app.urls'))
]
urls.py (myapp)
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('about/', views.about, name="about")
]
In index.html, everything is working well, so i will not put the code.
and in view.py:
from django.shortcuts import render
import requests
def about(request):
return render(request, "about.html")
i have the code below in my index.html:
About
and i cannot get about.html page when click About link as you can see above.
Can anybody help me?
Thanks in advance..
You have to use this tag to avoid manual rendering of urls.
In your code:
About
Let me know if this works!

Django: how to access functions in the views.py

I have a few functions in the view.py in my Django project:
Here is the views.py and urls.py under polls:
polls/views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
def search(request):
return HttpResponse("You're at the polls search.")
polls/urls.py
from django.urls import path
from . import views
from django.conf.urls import include, url
urlpatterns = [
path('', views.index, name='index'),
path('', views.search, name='search'),
]
I am able to get the index page, but have trouble to reach the page in the search function. I got the error below:
How do I access the search function in the views.py? Thanks!
edit your polls/urls.py as below:
urlpatterns = [
path('', views.index, name='index'),
path('search/', views.search, name='search'),
]
the first argument of the path is the url pattern.
I think you misunderstood the third argument(name). it has nothing to do with the url pattern, it's a name for the url, that'll be useful for url reversing. read the document for more information

no module named search after adding project url

I've had the pleasure to work with somebody yesterday on the issue with my urls here Adding an additional template to an existing project errors out, but after trying everything suggested i'm still in the same situation.
My project is named mysite and my application is search.
It was suggested to add the following to my project urls.py
url(r'^search/', include('search.urls')),
When doing so I'm given the error ModuleNotFoundError: No module named 'search'.
My project urls.py is the following:
from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import TemplateView
from django_filters.views import FilterView
from mysite.search.filters import UserFilter
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name='home.html'), name='home'),
url(r'^search/$', FilterView.as_view(filterset_class=UserFilter, template_name='search/user_list.html'), name='search'),
url(r'^admin/', include(admin.site.urls)),
url(r'^search/', include('search.urls')),
]
I'm attempting to add the following to my app urls.py
from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import TemplateView
from django_filters.views import FilterView
from mysite.search.filters import UserFilter
from . import views
urlpatterns = [
url(r'^results/$', views.results, name='results'),
]
I have an empty view for results defined as
def results(request):
return render(request, 'results.html')
When I try to add the following POST to my form for the results it gives me the error in the first post. When I have the results url in my app.urls.py
<form action = "{% url 'results' %}" form method = "POST">
This is what my current application structure looks like. Please help get me on the right track. Thank you.
Your search directory is in your mysite directory (the one that includes settings.py. That means you should include mysite.search.urls (just as you use mysite.search in your import and INSTALLED_APPS).
from mysite.search.filters import UserFilter
urlpatterns = [
...
url(r'^search/', include('mysite.search.urls')),
]
If your search directory was in your project directory (the one that includes manage.py, then you would remove mysite from the import, include() and INSTALLED_APPS.
from search.filters import UserFilter
urlpatterns = [
...
url(r'^search/', include('search.urls')),
]