While still not a lot of articles on the new site, I add RSS-feed, the benefit of that Django has a built-in functionality for organizing RSS-feeds, as in the usual format, and the Atom format. But first, usually confine adding news feeds, which can be connected to the service by FeedBurner, and also found on the site some RSS-reader. For example, I use QuiteRSS , which, incidentally, is written in Qt5.
What is needed for organizing RSS-feeds in the minimum version:
- Add a reference to the feed in the body of the page head-tag;
- Write view, which will be responsible for the preparation of the feed;
- Modify a model for the projects which will be based newswire.
Adding links to RSS-feed
First, add a link to the RSS-feed is in the page header. Why do it? - It is necessary to ensure that RSS-readers can easily find the feed when parse your site. If you do not provide a link, the user will have to manually search your RSS-feed.
<head> ... <link rel="home" type="application/rss+xml" href="http://example.com/feed" /> ... </head>
In this case, RSS-feed contains at feed.
Implementation of view
To implement the presentation we need a Feed class, and import that model, the objects which will be based newswire. Naturally chosen the Article model, about which I have already told in the article on the models, templates and views that are used on this site.
from django.contrib.syndication.views import Feed from knowledge.models import * class ArticlesFeed(Feed): title = "EVILEG - Practical programming" description = "Recent Articles EVILEG site about programming and information technology" link = "/" def items(self): return Article.objects.exclude(article_status=False).order_by('-article_date')[:10] def item_title(self, item): return item.article_title def item_description(self, item): return item.article_content[0:400] + "<p>The article first appeared on <a href="\"https://evileg.com/en\"">EVILEG " \ "- Practical programming</a></p>"
So, in order to take advantage of the RSS-feed, you must inherit from the class Feed. Override the title field, which will be the name of your RSS-channel to override the description field, which is a description of your RSS-channel, as well as specify the channel address, but since I have this channel is the main page of the site, the link is specified as a slash.
NOTE: If you do not specify the link even so, will crumble errors and nothing will not work.
items method returns the 10 most recent articles, excluding those that have the status of a draft.
item_title method respectively substitutes the name of the article.
item_description method substitutes the description of articles in the field article_content model. In the description enter the first 400 characters in the article and additional signature with a link back to the site. Contact link to the site is made to news aggregators, which will feed on your website leave back links to the website at.
Modification Article Model
def get_absolute_url(self): return reverse('knowledge:article', kwargs={'section': self.article_section.section_url, 'article_id': self.id})
Articles are in knowledge module and divided into sections. Therefore, to form a complete address is used two variables:
- address of section
- id of article
URL pattern is as follows:
url(r'^(?P[\w]+)/(?P<article_id>[0-9]+)/$', views.EArticleView.as_view(),
Adding a URL pattern for RSS
And to make it work, it is necessary to add a URL pattern for RSS. I have this template is in the home module, as well as all news feed, in addition to the model articles. And this pattern looks as follows.
from django.conf.urls import url from . import views urlpatterns = [ url(r'^feed/$', views.ArticlesFeed()), ]
For
Django
I recommend
VDS-server of Timeweb hoster
.
Тут мне тоже есть что сказать=)
Сами разрабы советуют импортировать следующим образом:
Стоит избегать
или
в первом случае некоторым IDE срывает крышу=) (а вообще это несколько опасно, особенно в случае с джангой - можно импортнуть не то и не оттуда)
а во втором может случиться коллизия, например в двух модулях есть классы с одним и тем же именем (ну мало ли). и вот не угадаешь, какой будет в итоге=)
Явное лучше неявного=)
Что интересно, если написать так
,то PyCharm сносит крышу, если разрабатываешь в рамках проекта приложение, которое подготавливается в качестве самостоятельного приложения, которое в дальнейшем можно будет устанавливать через pip . То есть если оно имеет структуру гит репозитория, как, например, evileg_core
PyCharm вообще не понимает, что происходит.
приходится писать так