I'm currently trying to create a homepage for Django, but when I test it, it just shows me a blank page with nothing on it. This is what I have so far. First is my URL page from mysite:
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'',include('firstapp.urls')),
]
and my url page from firstapp:
from django.conf.urls import url
from firstapp.views import HomePage
urlpatterns = [
url(r'^$', HomePage.as_view(), name='home'),
]
this is my views page:
from django.shortcuts import render
from django.views.generic import TemplateView
class HomePage(TemplateView):
template_name = 'home/home.html'
def home(request):
return render(request, self.template_name)
this is the base.html which extends to home.html
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Home</title>
<link rel='stylesheet' href='{% static "css/base.css" %}'/>
</head>
<html>
<p>{{ variable }}</p>
<script src= '{% static "js/base.js" %}'></script>
</body>
</html>
And finally home.html itself.
{% extends 'base.html' %}
{% block body %}
<div class="container">
<p>Testing Home</p>
</div>
{% endblock %}
Now if it works, I should be seeing "Testing Home"...but...I see nothing. I just get an empty white page. Any idea why this is happening?
Your base.html doesn't have a {% block body %} defined anywhere in it. You need blocks to be defined in the template you're extending, otherwise you'll have nothing to override inside the extended template.
base.html
<html>
<body>
<p>{{ variable }}</p>
{% block body %}{% endblock %}
<script src= '{% static "js/base.js" %}'></script>
</body>
</html>
Note: I also fixed your opening <body> tag which you incorrectly had as <html>, leaving you with two <html> opening tags in your template.
Related
I am trying to use a template html file for my password reset form. But it is not redirecting to my template file rather it is redirecting ti Django administration page. I don't want to use django administration page for resetting the password change link.
password_reset_form.html:
<!DOCTYPE html>
{% load static %}
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
<link rel="stylesheet" href="{% static 'employeeregistration/css/master.css' %}">
</head>
<body>
{% load bootstrap4 %}
<div class="container title">
<h2 style="text-align:center; padding-top:100px;">Reset your password</h2>
</div>
<div class="container">
<form method="POST" action="{% url 'password_reset' %}">
{% csrf_token %}
{{ form.as_p }}
{% buttons %}
<button type="submit" class="btn btn-primary">Send confirmation mail</button>
{% endbuttons %}
</form>
</div>
</body>
</html>
urls.py:
from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
app_name = 'registration'
urlpatterns = [
path('login/', auth_views.LoginView.as_view(template_name="registration/login.html"), name='login'),
path('password_reset/', auth_views.PasswordResetView.as_view(template_name="registration/password_reset_form.html"), name='password_reset'),
# path('password_reset/password_reset_done/', auth_views.PasswordResetView.as_view(template_name="registration/password_reset_done.html"), name='password_reset_done'),
path('logout/', auth_views.LogoutView.as_view(), name='logout'),
path('signup/', views.EmployeeSignUp.as_view(), name='signup'),
]
views.py:
from django.contrib.auth import login, logout
from django.contrib.auth.mixins import(LoginRequiredMixin,
PermissionRequiredMixin)
from django.views.generic import (TemplateView,ListView,
DetailView,CreateView,
UpdateView,DeleteView)
from django.urls import reverse_lazy
from django.views import generic
from django.views.generic import CreateView
from . import forms
class EmployeeSignUp(CreateView):
"""
Views for employee sign up
"""
form_class = forms.NewEmployeeRegistrationForm
success_url = reverse_lazy("login")
template_name = 'registration/signup.html'
class AdminView(TemplateView):
template_name = 'registration/admin.html'
project urls.py:
from django.contrib import admin
from django.urls import path, include
# from django.contrib.auth import views
from . import views
urlpatterns = [
path('', views.HomePage.as_view(), name='home'),
path('admin/', admin.site.urls),
path('about/', views.AboutView.as_view(), name='about'),
path('registration/', include("django.contrib.auth.urls")),
path('registration/', include('registration.urls', namespace='registration')),
# path('registration/password_reset', include('registration.urls', namespace='password_reset')),
# path('registration/password_reset_done', include('registration.urls', namespace='password_reset_done')),
]
Since you have specified a namespace, in the action attribute in password_reset_form.html, you have to first specify the name of the namespace the name of the view separated by a colon. You can find the appropriate documentation here
So you have to change password_reset_form.html to-
<!DOCTYPE html>
{% load static %}
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
<link rel="stylesheet" href="{% static 'employeeregistration/css/master.css' %}">
</head>
<body>
{% load bootstrap4 %}
<div class="container title">
<h2 style="text-align:center; padding-top:100px;">Reset your password</h2>
</div>
<div class="container">
<form method="POST" action="{% url 'registration:password_reset' %}">
{% csrf_token %}
{{ form.as_p }}
{% buttons %}
<button type="submit" class="btn btn-primary">Send confirmation mail</button>
{% endbuttons %}
</form>
</div>
</body>
</html>
Also you have two paths in your root project urls.py which lead to the same url. So consider changing it to
from django.contrib import admin
from django.urls import path, include
# from django.contrib.auth import views
from . import views
urlpatterns = [
path('', views.HomePage.as_view(), name='home'),
path('admin/', admin.site.urls),
path('about/', views.AboutView.as_view(), name='about'),
# path('registration/', include("django.contrib.auth.urls")),
path('registration/', include('registration.urls', namespace='registration')),
# path('registration/password_reset', include('registration.urls', namespace='password_reset')),
# path('registration/password_reset_done', include('registration.urls', namespace='password_reset_done')),
]
I have commented out one url pattern which might be causing the issue.
I´m working on a course exercise and I'm stuck for a few hours and I'm not sure what is causing the app to break, next, you will find the files involved and perhaps you can find out the solution. Thanks for your help!
Project structure
This error is being thrown when I log in:
Internal Server Error: /account/login/
...
django.urls.exceptions.NoReverseMatch: Reverse for 'dashboard' not found. 'dashboard' is not a valid view function or pattern name.
[04/Apr/2018 17:12:15] "POST /account/login/ HTTP/1.1" 500 151978
At the end of the settings.py file
from django.urls import reverse_lazy
LOGIN_REDIRECT_URL = reverse_lazy('dashboard')
LOGIN_URL = reverse_lazy('login')
LOGOUT_REDIRECT_URL = reverse_lazy('logout')
The urls.py file
from django.contrib.auth import views as auth_views
from django.urls import path
from . import views
app_name = 'account'
urlpatterns = [
# path('login/', views.user_login, name='login'),
path('', views.dashboard, name='dashboard'),
# login / logout urls
path('login/', auth_views.LoginView.as_view(template_name='registration/login.html'), name='login'),
path('logout/', auth_views.LogoutView.as_view(template_name='registration/logged_out.html'), name='logout'),
path('logout-then-login/', auth_views.logout_then_login, name='logout_then_login'),
]
The views.py file
from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.shortcuts import render
#login_required
def dashboard(request):
return render(request, 'account/dashboard.html', {'section': 'dashboard'})
The base.html template
{% load staticfiles %}
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" href="{% static "css/base.css" %}">
</head>
<body>
<div id="header">
<span class="logo">Bookmarks</span>
{% if request.user.is_authenticated %}
<ul class="menu">
<li> {% if section == "dashboard" %}class="selected"{% endif %}>My dashboard</li>
<li> {% if section == "images" %}class="selected"{% endif %}Images</li>
<li> {% if section == "people" %}class="selected"{% endif %}People</li>
</ul>
{% endif %}
<span class="user">
{% if request.user.is_authenticated %}
Hello {{ request.user.first_name }}, Logout
{% else %}
{% endif %}
</span>
</div>
<div id="content">
{% block content %}
{% endblock %}
</div>
</body>
</html>
I appreciate your help. Thanks a lot!
You've set a namespace for your urls:
app_name = 'account'
You need to use that namespace when reversing urls with reverse/reverse_lazy or {% url %}:
LOGIN_REDIRECT_URL = reverse_lazy('account:dashboard')
LOGIN_URL = reverse_lazy('account:login')
LOGOUT_REDIRECT_URL = reverse_lazy('account:logout')
Perhaps when specifying {% url 'appname:views' %} you specified the wrong appname
For example, like:
wrong - {% url 'accuant:dashboard' %}
right - {% url 'account:dashboard' %}
This is my code and I receive error on index page ** {% for post in posts %} Error during template renderingn template /home/elite/Downloads/django/FirstBlog/blog/templates/index.html, error at line 13
index.html
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”utf-8”>
<title>Awesome HTML5 Webpage</title>
<meta name=”description” content=”An awesome HTML5 page YOU built from scratch!”>
<meta name=”author” content=”Udemy”>
<link rel=”stylesheet” href=”style.css”>
</head>
<body>
<div class='cointainer'>
<h1>First Blog</h1>
{% for post in posts %} <<<<- This where error I get--
<h2>{{ post.tittle }}</h2>
<h3>Posted{{ post.timestamp }} by {{ post.author }}</h3>
<p>{{post.bodytext }} </p>
{% endfor %}
</div>
</body>
</html>
views page
from django.shortcuts import render
from blog.models import posts
def home(request):
entries = posts.objects.all()[:10]
return render(request, 'index.html',{'posts' : entries})
This is url page
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
# Examples:l()
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'blog.views.home', name='home'),
)
I keep getting a 'AttributeError at /home/ 'list' object has no attribute 'resolve'' error. I've changed the code in multiple ways and read the docs, but I'm still confused.
'python manage.py findstatic /file/css/syle.css' didn't help much either.
from urls.py:
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import patterns, include, url
from django.contrib import admin
from home import views
admin.autodiscover()
urlpatterns = [ patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^home/$', views.Home, name="home"),
url(r'^services/$', views.Services, name="services"),
url(r'^contact/$', views.Contact, name="contact"))
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
from settings.py:
STATIC_URL = 'home/static/'
STATIC_ROOT = 'sitename/home/static/css.js'
STATIC_DIRS = 'home/static'
project structure:
sitename
db.sqlite3
home
__init.py
admin.py
models.py
static
home.html
services.html
contact.html
views.py
manage.py
mysite
__init.py
settings.py
urls.py
wsgi.py
What am I missing?
Also, I'd love your recommendations for reference material.
from home.html:
{% load staticfiles %}
{% block doctype %}<!DOCTYPE HTML>{% endblock %}
{% load i18n %}
<html>
<head>
<title>{% block title %}{% endblock %}{% trans "title" %}</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
{% block meta_tags %}{% endblock %}
<noscript>
<link rel="stylesheet" href="css/skel.css" />
<link rel="stylesheet" href="css/style.css" />
<link rel="stylesheet" href="css/style-wide.css" />
</noscript>
{% block stylesheet %}{% endblock %}
<script src="js/jquery.min.js"></script>
<script src="js/jquery.doc.min.js"></script>
<script src="js/doc.min.js"></script>
<script src="js/doc-layers.min.js"></script>
<script src="js/init.js"></script>
{% block js %}{% endblock %}
</head>
<body class="{% block bodyclass %}{% endblock%}">
{% block page %}
<div id="header">{% block header_navigation %}
<h1>{% trans "header/name of business" %}<em>content description</em></h1>
<nav id="nav">
<ul>
<li class="current">Home</li>
<li>Services</li>
<li>Contact Us</li>
</ul>
</nav>
{% endblock %}
</div>
<section class="wrapper style1">
<div class="container">
<div class="row 200%">
<section>
<p>content</p>
<p>content</p>
<p>content</p>
</section>
</div>
</div>
</section>
<div id="footer">
{% block footer %}
<div class="container">
<div class="row">
<section class="3u 6u(narrower) 12u$(mobilep)">
</section>
{% endblock %}
</div>
</div>
<div class="copyright">
<ul class="menu">
<li>© All rights reserved</li><li>Adapted by: me</li>, Original Design: someone else</li>
</ul>
</div>
</div>
{% endblock %}
</body>
</html>
You should remove square brackets around the patterns() and add staticfiles_urlpatterns() to the urls.py:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^home/$', views.Home, name="home"),
url(r'^services/$', views.Services, name="services"),
url(r'^contact/$', views.Contact, name="contact")
) + staticfiles_urlpatterns()
Change settings to:
STATIC_URL = '/static/'
STATIC_ROOT = '/absolute/path/to/static/dir/in/doc_root/'
# For example: /var/www/yoursite.com/static/
STATIC_ROOT should be set on production server. Development server ignores this setting.
STATIC_DIRS is not required for your project layout. Django automatically uses static directories in all apps from the INSTALLED_APPS as the source for static files.
To link to static assets you need to use the {% static %} template tag. For example instead of:
<link rel="stylesheet" href="css/style.css" />
code should be like this:
<link rel="stylesheet" href="{% static 'css/style.css' %}" />
BTW do not place templates into the static dir. manage.py collectstatic will make source code of these templates available for everyone. I suspect you don't want this :-)
the return type of static is a list, you should expand it:
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
*static(settings.STATIC_URL, document_root=settings.STATIC_ROOT),
]
I am new to Django and I am practicing template inheritance. I am currently having trouble inheriting templates on the 3rd level. The base level is a template that my whole site uses (ex: navbars). The second level is the content of my site. However this content is a bit lengthy so I took a portion(contactform.html) of it and created its own HTML file for that portion.
I am able to get my home.html into my index.html like so
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head lang="en">
<link href="{% static "css/boothie.css" %}" rel="stylesheet" type="text/css">
<script src="{% static "js/boothie.js" %}"></script>
<script src="{% static "js/jquery.easing.1.3.js" %}"></script>
<title>Boothie</title>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
Within my home.html I want to include my contactform.html. This is what I have so far.
{% extends "index/index.html" %}
{% load staticfiles %}
{% block content %}
...
...
...stuff...
<!-- contact -->
{% block contactform %}{% endblock %}
{% endblock %}
My contactform.html:
{% extends "home/home.html" %}
{% load staticfiles %}
{% block contactform %}
<section id="contact">
<!-- HTML! -->
</section>
{% endblock %}
This is what is currently in my home/views.py:
from django.shortcuts import render
from django.views import generic
class HomeView(generic.TemplateView):
template_name = "home/home.html"
my home/urls.py:
from django.conf.urls import patterns, url
from home.views import HomeView
urlpatterns = patterns('',
url(r'^$', HomeView.as_view(), name="home"),
)
TEMPLATE_DIRS:
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
os.path.join(BASE_DIR, 'home'),
)
here is a picture of my project structure:
Instead of inheriting, just include the contactform template in the home template. In home/home.html put:
<section id="contact">
{% include 'home/contactform.html' %}
</section>
You dont need to extend, instead include
Example, keep the contents of the contact_form.html with just the required html content (without the extends, and the block tag, etc..), and then include the html snippet. Now, django would do the magic for you - The included snippet would have all the context variables too.
{% extends ".." %}
{% load staticfiles %}
{% block content %}
...
...
...stuff...
{% include /path/to/contactform.html %}
{% endblock %}