app.views.py
from django.shortcuts import render
from django.views.generic import View
class RegisterView(View):
def get(self,request):
return render(request, 'register.html' , { 'title' : 'Register Page'} );
register.html
{% extends 'base.html' }
{% block head}
{{title}}
{% endblock}
{% block content}
Register Page
{% endblock }
base.html
Arena Application
{% block head}
Main Page
{% endblock }
</title>
</head>
<body>
<h2> Welcome to arena app </h2>
{% block content }
<p>
this is base page
</p>
{% endblock }
</body>
</html>
**Problem : ** When i visit http://127.0.0.1:8000/account/register , this is what i get :
http://i.stack.imgur.com/Y9Lod.png
My file Structure :
http://i.stack.imgur.com/1KvDP.png
And finally my TEMPLATE_DIRS var in settings.py looks like:
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
Django version : 1.7.3
The code is not escaped properly. Should start and end with {% and %}
Example:
{% endblock %}
not
{% endblock}
Related
I am relatively new to Django and started to create my first To-Do-List.
However I get an error whenever I try to create an href that says: NoReverseMatch at /aufgabenzettel/
I have desperately tried to fix it for the last five hours and am very frustrated because the error just seems to be caused by a single line of code... Please help! It would be awesome and I really appreciate every hint!
Here's the code:
urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("<int:aufgabenzettel_id>", views.details, name="details")
]
views.py
from django.shortcuts import render
from .models import Aufgabenzettel
# Create your views here.
def index(request):
return render(request, "aufgabenzettel/index.html", {
"Aufgabenliste":Aufgabenzettel.objects.all()
})
def details(request, aufgabenzettel_id):
aufgabenzettel = Aufgabenzettel.objects.get(pk=aufgabenzettel_id)
return render(request, "aufgabenzettel/details.html", {
"details":aufgabenzettel
})
models.py
from django.db import models
# Create your models here.
class Aufgabenzettel(models.Model):
Aufgabeselbst = models.CharField(max_length=64)
def __str__(self):
return f"{self.Aufgabeselbst}"
layout.html
<!DOCTYPE html>
<html lang="de">
<head>
<title>Aufgabenzettel</title>
</head>
<body>
{% block body %}
{% endblock %}
</body>
</html>
index.html
{% extends "aufgabenzettel/layout.html" %}
{% block body %}
<h1>Meine Aufgaben</h1>
<ul>
{% for Aufgabeselbst in Aufgabenliste %}
<li>
<a href="{% url 'details' aufgabenzettel.id %}">
Aufgabe {{ Aufgabeselbst }}
</a>
</li>
{% endfor %}
</ul>
{% endblock %}
details.html
{% extends "aufgabenzettel/layout.html" %}
{% block body %}
<h1> Aufgabe {{ details }}</h1>
Zurück zu Aufgabe
{% endblock %}
The exact error:
Reverse for 'details' with arguments '('',)' not found. 1 pattern(s) tried: ['aufgabenzettel/(?P<aufgabenzettel_id>[0-9]+)$']
Whenever I delete the line <a href="{% url 'details' aufgabenzettel.id %}"> in index.html the programme works perfectly fine...
Let me know if you need some more information!!
I really appreciate your help!
I'm Developing an app with django .
I want to customize django admin interface , but i cant add a custom font to it .
I Want to use a custom font for Persian Language .
Here is What i did but not get a correct result :
Step 1 :
I create a css file named admin-extra.css in this directory :
/templates/css/admin-extra.css
After That i Changed the postition of myappname before django.contrib.admin in Installed Apps Like This :
INSTALLED_APPS = [
'django.contrib.auth',
'pool',
'django.contrib.admin',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'fcm_django'
]
And the admin-extra.css is like this :
body {
margin: 0;
padding: 0;
font-size: 40px;
color: #333;
background: #fff;
}
And Finally I put admin-extra in a file named base_site.css in \templates\base_site.html and it's content is like this :
{% extends "admin/base.html" %}
{% load static from staticfiles %}
{% block title %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %}
{% block extrastyle %}{{ block.super }}<link rel="stylesheet" type="text/css" href="{% static "css/admin-extra.css" %}" />{% endblock %}
{% block branding %}
<h1 id="site-name">{{ site_header|default:_('Django administration') }}</h1>
{% endblock %}
{% block nav-global %}{% endblock %}
But i cant see the result ,
what i've been missed or wrong ?
any suggestions will be helpfull .
Note : This is not a duplicate post .
And This is Admin.py file :
from django.contrib import admin
# Register your models here.
from .models import *
#admin.register(Ticket)
class TicketAdmin(admin.ModelAdmin):
list_display = ('id','title','body','answer')
#admin.register(Activation)
class ActivationAdmin(admin.ModelAdmin):
list_display = ('activecode','user_phone','createtime')
list_filter = ('activecode','user_phone','createtime')
search_fields = ('activecode','user_phone','createtime')
#admin.register(App)
class AppAdmin(admin.ModelAdmin):
list_display = ('version','versionurl','bonprice','rahnama')
list_filter = ('version','versionurl','bonprice','rahnama')
search_fields = ('version','versionurl','bonprice','rahnama')
#admin.register(Bid)
class BidAdmin(admin.ModelAdmin):
list_display = ('user','bidtime','competition','maxbon','bonnumber')
list_filter = ('user','bidtime','competition','maxbon','bonnumber')
search_fields = ('user','bidtime','competition','maxbon','bonnumber')
You should override the admin.py file.
from django.contrib import admin
class MyModelAdmin(admin.ModelAdmin):
class Media:
css = {
'all': ('/templates/css/admin-extra.css ',)
}
admin.site.register(MyModel,MyModelAdmin)
You need to add base_site.html as follows:
templates
admin
base_site.html
with:
{% extends "admin/base.html" %}
{% load static from staticfiles %}
{% load i18n grp_tags %}
{% block title %}{{ title }} | {% get_site_title %}
{% endblock %}
{% block extrastyle %}
<link rel="stylesheet" type="text/css" href="{% static "css/admin-extra.css" %}" />
{% endblock %}
{% block branding %}
{# Use the div#grp-branding for branding elements, e.g. a logo #}
{# <div id="grp-branding"></div> #}
{% endblock %}
{% block nav-global %}
{% endblock %}
I am not sure if you still have this problem or if the versions make things different, but here is what I thought could solve the problem.
https://docs.djangoproject.com/en/3.0/howto/static-files/
According to the documentation above, .css as a static file is placed in different directory as .htmls. If you have the same configuration as official example, you can create a directory called "static" under your app folder, and put your css files there.
{% static %} points to that folder. So if you write {% static "css/admin-extra.css" %} in your html, your css should be in APP_FOLDER/static/css/admin-extra.css.
Hope it works.
In the static folder of my DjangoServer is located a template of the default webpage. It's decorated with some template blocks.
If I load this template file, the path to the template is shown in the browser, it looks like, that the code is not loaded.
If I store the template in an app/template folder and I extend this file. It works very well. I use the tutorial of Django but it still not working.
Settings.py
django.contrib.staticfiles is added to INSTALLED_APPS
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
projectRootFolder/static/html/basePage.html
{% load static from staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
{% block head %}
<title>{% block title %}My amazing site{% endblock %}</title>
{% endblock %}
</head>
<body>
{% block body %}
<header>
{% block header %}
<header> -- HEADER BANNER --</header>
{% block menu %}<nav></nav>{% endblock %}
{% endblock %}
</header>
<section>
{% block section %} SECTION {% endblock %}
</section>
{% block footer %}
<footer> -- FOOTER --</footer>
{% endblock %}
{% endblock %}
</body>
<script type="text/javascript" src="{% static 'angularjs/SOME_ANGULAR_FILES_LOADED.js' %}"></script>
</html>
app/template/app/index.html from an app
{% load static from staticfiles %}
{% static "html/basePage.html" %}
{% block menu %}<nav>App A</nav>{% endblock %}
{% block section %} Lorem Ipsum{% endblock %}
app/views.py
from django.template import loader
from django.shortcuts import render
from django.http import HttpResponse
# PAGE CALLS
def index(request):
template = loader.get_template('mainControll/index.html')
context = {}
return HttpResponse(template.render(context, request))
Output
What is the mistake i've made? How can I load this template correctly?
Warning, this answer assumes your goal for asking the question is to get things to work, instead of helping you troubleshoot a probable permission problem just so that you can run into another problem. That is, I'm assuming your end goal is not a html document inside a non-html document.
What can you do to get your output to work:
It looks like you want to include a template into another template, you can do that with {% extends "basePage.html" %}. Your template does then need to be at a location where templates are found, not where static pages are found.
E.g. If your app is called 'myapp' then under myapp/templates/ is a one possible place, assuming the TEMPLATES setting has APPDIRS = True
This would mean changing index.html to be
{% extends "basePage.html" %}
{% block menu %}<nav>App A</nav>{% endblock %}
{% block section %} Lorem Ipsum{% endblock %}
See https://tutorial.djangogirls.org/en/template_extending/ for example of this and https://docs.djangoproject.com/en/1.11/topics/templates/#configuration for configuring things to that your basePage.html can be found
I'm transferring my website from PHP to Django, and now I have to make a sidebar.
I want the entries to come from the DB, and than I want them to become hyperlinks for other pages..
How can I do that?
nav.html
<nav class="menu" id="theMenu">
<div class="menu-wrap" data-spy="scroll">
<h1 class="logo">MY BOOKS</h1>
<i class="icon-remove menu-close"></i>
{% for question in latest_question_list %}
{{ question.naslov }}
{% endfor %}
</div>
<div id="menuToggle"><i class="icon-reorder"></i></div>
</nav>
master2.html
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}{% endblock %}</title>
<link href="/static/font.min.css" rel="stylesheet">
<link href="/static/bootstrap.min.css" rel="stylesheet">
<link href="/static/font-awesome.min.css "rel="stylesheet">
<link href="/static/main.css" rel="stylesheet">
</head>
<body data-spy="scroll" data-offset="0" data-target="#theMenu">
{% include "nav.html" %}
{% include "header2.html" %}
{% block h1 %}{% endblock %}
<script src="/static/jquery.js"></script>
<script src="/static/bootstrap.min.js"></script>
<script src="/static/jquery.isotope.min.js"></script>
<script src="/static/jquery.prettyPhoto.js"></script>
<script src="/static/main2.js"></script>
</body>
</html>
views.py
from django.shortcuts import render
from .models import Question
def index(request):
latest_question_list = Question.objects.all()
context = {'latest_question_list': latest_question_list}
return render(request, 'papers/index.html', context)
def detail(request, slug):
question = Question.objects.get(slug=slug)
return render(request, 'papers/detail.html', {'question': question})
urls.py
from django.conf.urls import include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin
urlpatterns = [
url(r'^$', 'papers.views.index', name='index'),
url(r'^admin/', include(admin.site.urls)),
url(r'^(?P<slug>[\w_-]+)/$', 'papers.views.detail', name='detail'),
]
urlpatterns += staticfiles_urlpatterns()
detail.html
{% extends "master2.html" %}
{% block h1 %}
<div id="g">
<div class="container">
<div class="row">
<h3>{{ question.naslov }}</h3>
<br>
<br>
<div class="col-lg-6 desc "><p>{{ question.opsirnije_text }}</p></div>
<div class="col-lg-4 desc desc-b">
<p>{{ question.opsirnije_text }}</p>
</div>
</div>
</div>
</div>
{% endblock %}
{% block title %} Detail {% endblock %}
You might be looking for the for template tag.
{% for question in latest_question_list %}
{{ question.naslov }}
{% endfor %}
Now, if you want the list of questions to show up on all (or almost all) pages, you could write a template context processor that adds them to all contexts.
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 %}