Answer the question
In order to leave comments, you need to log in
Template inheritance in Django?
Ok, I have a Blog project with an Article application inside it. In Article
I have a templates folder, this folder contains articles.html, with the following content:
{% extends to 'main.html'%}
{% block articles %}
{% for article in articles %}
<h1><a href="/articles/get/{{article.id}}">{{article.article_title}}</a></h1>
<h2>{{article.article_text}}</h2>
<h3>Дата публикации:{{article.article_date}}</h3>
<h4>Лайков:{{article.article_likes}}</h4>
<hr/>
{% endfor %}
{% endblock %}
from django.shortcuts import render
from django.shortcuts import render_to_response
from django.http.response import HttpResponse
from article.models import Article, Comments
def articles(reguest):
return render_to_response('articles.html', {'articles': Article.objects.all()})
def article (reguest, article_id=1):
return render_to_response('article.html', {'article': Article.objects.get(id=article_id), 'comments': Comments.objects.filter(comments_article_id=article_id)})
from article import views
from django.conf.urls import url, include
urlpatterns = [
url(r'^$', views.articles, name='articles'),
url(r'^get/(?P<article_id>\d+)/$', views.article, name='article')
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question