Django Open rendered HTML file - python

Guys i want to open rendered HTML file when i am clicking a image on my HTML page. How i should use it?
HTML code fragment
<header>
<nav class="headlist">
<ul>
<li>O nas</li>
<li>Kontakt</li>
<li><a>Zajęcia</a></li>
</ul>
</nav>
</header>
main app urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('pages.urls')),
]
pages app urls.py
from django.urls import path
from . import views
urlpatterns = [
path('kontakt/', views.contact_view),
path('o_nas/', views.about_view),
path('', views.home_view),
]
pages app views.py
from django.shortcuts import render
def home_view(reqest):
return render(reqest, "index.html")
def about_view(reqest):
return render(reqest, "about.html")
def contact_view(reqest):
return render(reqest, "contact.html")

First things first: add a name to each route inside pages app urls.py.
pages app urls.py
from django.urls import path
from . import views
urlpatterns = [
path('kontakt/', views.contact_view, name='contact'),
path('o_nas/', views.about_view, name='about'),
path('', views.home_view, name='home'),
]
Inside the HTML Template you can use the url tag in combination with the name of the route to assign the desired target to the href.
HTML code fragment
<header>
<nav class="headlist">
<ul>
<li>O nas</li>
<li>Kontakt</li>
<li><a>Zajęcia</a></li>
</ul>
</nav>
</header>

Related

Django url pattern is not a registered view function or pattern

I have a small Django web app, with multiple applications inside them. I have used the include in the urls.py files, but whenever I reference the URLs in the HTML files they don't load. Below are my 3 urls.py files. The one I'm having an issue will specifically is the nodes url pattern in the nodes urls.py
#main urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('account/', include('account.urls')),
]
#account urls.py
from django.urls import path, include
from django.contrib.auth import views as auth_views
from . import views
app_name = 'account'
urlpatterns = [
path('login/', auth_views.LoginView.as_view(), name='login'),
path('logout/', auth_views.LogoutView.as_view(), name='logout'),
path('dashboard/', views.dashboard, name='dashboard'),
path('nodes/', include('nodes.urls')),
]
#nodes urls.py
from django.urls import path
from . import views
app_name = 'nodes'
urlpatterns = [
path('', views.nodes, name='nodes'),
]
This is my HTML file where I am referencing the URL pattern:
<li {% if section == 'nodes' %}class="active"{% endif %}>
<a class="nav-link" href="{% url 'nodes' %}">Nodes</a>
</li>
Due to this pattern name you set in urls file if change HTML file to this it must be fixed:
<li {% if section == 'nodes' %}class="active"{% endif %}>
<a class="nav-link" href="{% url 'account:nodes:nodes' %}">Nodes</a>
</li>
just for tips :) if import urls file at the django shell and print list of urlspatterns you could see default name assign to your nodes path.

NoReverseMatch at / in Django How to pass url

<a class="nav-link active" aria-current="page" href="{% url 'tasks' %}">Tasks</a>
When I'm using this template tag I get an error no ReverseMatch.
but ,
<a class="nav-link active" aria-current="page" href="/tasks">Tasks</a>
When I'm doing this I get no error.
Can someone explain me?
urls.py of the project
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('tracker.urls')),
path('tasks/', include('tasks.urls')),
]
urls.py of the tasks app
from django.urls import path
from . import views
urlpatterns = [
path('', views.tasklist, name="tasklist"),
]
views.py of the tasks app
from django.shortcuts import render
def tasklist(request):
return render(request, 'tasks/task_list.html')

Reverse for 'display_data' not found. 'display_data' is not a valid view function or pattern name

I am getting the below error when I am trying to load the home page:
Reverse for 'display_data' not found. 'display_data' is not a valid view function or pattern name
My views.py file is as follows:
def home(request):
#query_results = QRC_DB.objects.all()
return render(request, 'display_data.html')
def display_data(request,component):
#query_results = QRC_DB.objects.all()
return HttpResponse("You're looking at the component %s." % component)
My urls.py file under the app is as follows:
from django.urls import path
from fusioncharts import views
urlpatterns = [
path('home/', views.home, name=''),
]
The urls.py file under the project is as follows:
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('fusioncharts.urls'))
]
And my html file (display_data) code is as follows :
{% block content %}
<h3>Display the test results</h3>
<div id="container" style="width: 75%;">
<canvas id="display-data"></canvas>
<li>SQL</li>
</div>
{% endblock %}
Can anyone please help me to find out the mistake ?
Thanks.
Your urls.py file doesn't contain any url for display_data.
When you're trying to click the link rendered in the HTML tag, namely,
<li>SQL</li>
it tries to resolve the URL display_data.
First, it checks the root urls.py file. Among the following:
path('admin/', admin.site.urls),
path('', include('fusioncharts.urls'))
it matches the second one. Then it loads the fusioncharts.urls but the fusioncharts.urls doesn't contain any URL for display_data. That's why you are getting the error.
The urls.py file should be like this:
from django.urls import path
from fusioncharts import views
urlpatterns = [
path('home/', views.home, name=''),
path('display_data/<str:arg>', views.display_data, name='display_data'),
]
# There is a change in urls.py and in your template 'display_data.html'
urls.py
urlpatterns = [
path('home/', views.home, name=''),
path('display_data/<str:component>', views.display_data, name='display_data'),
]
display_data.html
{% block content %}
<h3>Display the test results</h3>
<div id="container" style="width: 75%;">
<canvas id="display-data"></canvas>
<li>SQL</li>
</div>
{% endblock %}

Cannot Get image From Database Django Framework

I want to create a setting in admin panel which will help me to change the website logo from the admin panel. I create a model for database and it's working and uploading an image to the database + folder. but I cannot get this image to the Website template. when I'm viewing page source image SCR is empty.
my Model.py
class WebLogo(models.Model):
Logotitle = models.CharField(max_length=50,unique=False)
SiteLogo = models.ImageField(upload_to='webimages')
def __str__(self):
return self.Logotitle
my Views.py
from .models import Post,WebLogo
def Display_WebLogo(request):
# getting all the objects of hotel.
WebsiteLogo = WebLogo.objects.all()
return render((request, 'index.html',{'web_images' : WebsiteLogo}))
my project Urls.py
urlpatterns = [
path('', views.PostList.as_view(), name='home'),
path('<slug:slug>/', views.PostDetail.as_view(), name='post_detail'),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
my Html Code
<a href="index.html"><img src="{{ WebLogo.SiteLogo.url }}" class="img-fluid" alt="logo">
</a>
my app url
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')),
]
In HTML Code, use 'web_images' not WebLogo like this
<a href="index.html"><img src="{{ web_images.0.SiteLogo.url }}" class="img-fluid" alt="logo">
</a>
It looks like {{ data.0 }}. See Variables and lookups.
You should write the required information in the settings.py file.
https://docs.djangoproject.com/en/dev/ref/settings/#media-root

view function not found in django

When trying to click on a button to redirect to getonboard.html I get the following error:
django.urls.exceptions.NoReverseMatch: Reverse for 'getonboard' not found. 'getonboard' is not a valid view function or pattern name.
artdb/urls.py:
path('getonboard/',views.getonboard,name='getonboard'),
templates/artdb/base.html:
<p><a class="btn btn-primary btn-lg" href="{% url 'getonboard' %}" role="button">Get onboard »</a></p>
artdb/views.py:
def getonboard(request):
return render(request,'artdb/getonboard.html')
templates/artdb/getonboard.html:
{% extends "artdb/base.html" %}
{% block content %}
<h2>getonboard template</h2>
<p>this is the getonboard text</p>
{% endblock content %}
full main urls.py:
"""winmalist URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import include, path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('artdb/', include('artdb.urls')),
path('admin/', admin.site.urls),
]+static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
full artdb/urls.py:
from django.urls import path
from django.views.generic import TemplateView
from artdb.views import PersonList
from . import views
app_name='artdb'
urlpatterns = [
# path('base/',views.base,name='base.html'),
path('getonboard/',views.getonboard,name='getonboard'),
path('index/',views.index,name='index'),
path('persons/',PersonList.as_view()),
path('',TemplateView.as_view(template_name='artdb/base.html')),
path('test2',TemplateView.as_view(template_name='artdb/test2.html')),
# path('', views.IndexView.as_view(), name='index'),
# path('contract', views.contract, name='contract'),
# path('<int:person_id>/test1/', views.test1, name='test1'),
]
any thoughts?
When you include urlpatterns from a different app, you'll usually namespace those URLs by adding the line app_name = 'artdb' to the urls.py file inside the app. So, if the main urls.py file has the line:
urlpatterns = [
path('artdb/', include('artdb.urls')),
...]
And the artdb/urls.py file has these lines:
app_name = 'artdb'
urlpatterns = [
path('getonboard/', views.getonboard, name='getonboard'),
...]
then you can reverse this URL by namespacing the name:
reverse('artdb:getonboard')
This allows you to use the same name in multiple apps (e.g. index would be common), having artdb:index and user:index for example.

Categories