Пытаюся создать простой блог не не как не могу добовлять статьи по сути готовая версия уже есть но нужно сделать это все одним классом во vies.py что то типо этого
def all_articles(request):
articles = Article.objects.all()
return render_to_response('all_articles.html', {'articles': articles}, context_instance=RequestContext(request))
def one_article_by_slug(request, slug):
article = get_object_or_404(Article, slug=slug)
return render(request, 'one_article.html', {'article': article}, context_instance=RequestContext(request))
models.py
class Articles(models.Model):
title = models.CharField(max_length= 200)
post = models.TextField()
date = models.DateTimeField()
img = models.ImageField(upload_to='', default="default_value")
whoAreYou = models.CharField(max_length=100, default='Четко!')
textComment = models.TextField(blank=False, default='Четко!')
def __str__(self):
return self.title
post.html это сама статья
{% block content %}
<div class="panel panel-default">
<div class="panel-heading">
<h1 class=" text-info">{{articles.title}}</h1>
</div>
<div class="panel-body">
<p> {{articles.post|safe|linebreaks}} </p>
<h3 align="right" class=" text-info"> Опубликованно: {{articles.date|date:"d-m-Y в H:i"}}</h3>
</div>
<h4>Comments</h4>
<form action="" method="post">
{% csrf_token %}
{% if comments %}
{% for CommentModel in comments %}
{{ CommentModel.WhoAreYou }} <br>
{% endfor %}
{% endif %}
{{ form }}
<input type="submit" value="Submit">
</form>
{% endblock %}
posts.html здесь список статей
{% extends "ShapeHtml/wrapper.html" %}
{% block content %}
{% for article in articles_list %}
<div class="panel panel-default">
<div class="panel-heading">
<p><img src="{{ MEDIA_URL }}{{ article.img.url }}"/></p>
</div>
<div class="panel-body">
<a href="/news/{{article.id}}"> <h1> {{article.title}} </h1> </a>
<h3 align="right">{{article.date|date:"d-m-Y"}}</h3>
</div>
</div>
{% endfor %}
{% endblock %}
urls.py
urlpatterns=[
path('news/', ListView.as_view(queryset=Articles.objects.all().order_by("-date")[:20],template_name="news/posts.html")),
path('<int:pk>/', DetailView.as_view(model=Articles, template_name="news/post.html")),
path('aboutUs', views.aboutUs, name='aboutUs'),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
news/urls.py
path('', ArticlesList.as_view(), name='articles_list'),
path('<int:pk>/', ArticleDetail.as_view(), name='article_detail'),
views.py
class ArticlesList(ListView):
model = Article
template_name = 'news/posts.html'
class ArticleDetail(DetailView):
model = Article
template_name = 'news/post.html'
posts.html
{% extends "ShapeHtml/wrapper.html" %}
{% block content %}
{% for article in object_list %}
<div class="panel panel-default">
<div class="panel-heading">
<p><img src="{{ MEDIA_URL }}{{ article.img.url }}"/></p>
</div>
<div class="panel-body">
<a href="{% url 'article_detail' article.pk %}"> <h1> {{article.title}} </h1> </a>
<h3 align="right">{{article.date|date:"d-m-Y"}}</h3>
</div>
</div>
{% endfor %}
{% endblock %}
post.html
<div class="panel panel-default">
<div class="panel-heading">
<h1 class=" text-info">{{object.title}}</h1>
</div>
<div class="panel-body">
<p> {{object.post|safe|linebreaks}} </p>
<h3 align="right" class=" text-info"> Опубликованно: {{articles.date|date:"d-m-Y в H:i"}}</h3>
</div>
Продвижение своими сайтами как стратегия роста и независимости