djangorelishblog is a simple Django app to have a blog on your site.
- Add django.contrib.humanize, djangorelishblog and markdownx to your
INSTALLED_APPSsetting:
INSTALLED_APPS = [
...
'django.contrib.humanize',
'djangorelishblog',
'markdownx',
]- Include the djangorelishblog and mardownx URLconf in your project urls.py:
url(r'^blog/', include('djangorelishblog.urls')),
url(r'^markdownx/', include('markdownx.urls')),-
Run
python manage.py collectstaticto setup markdownx. -
Run
python manage.py migrateto create the blog models. -
Start the development server and visit http://127.0.0.1:8000/admin/ to create a blog (you'll need the Admin app enabled).
-
Visit http://127.0.0.1:8000/blog/ to view the blog.
author- ForeignKey toUSER_AUTH_MODELin settings.title- CharField with a max length of 255.slug- SlugField with a max length of 255.intro- MarkdownxField.body- MarkdownxField.published- BooleanField to say whether the post is viewable to everyone or not.created_on- DateField withauto_now_addset toTrue.
url- returns a string of the full URL of the post.
content()- concatenates theintroandbodyfields, and separates them with 2 line returns (\n\n).
The Post model uses a custom objects manager that adds a couple of handy methods.
Post.objects.published()- returns allPostobjects that are viewable by all.Post.objects.unpublished()- returns allPostobjects that are not viewable by all.
- If you use a User model other than the standard Django User model, add
USER_AUTH_MODEL = <model name>in your Django settings file. - When presenting the post, use the
content()as it will concatenate theintroandbodyfields, and separates them with 2 line returns (\n\n). - When saving a
Postobject, ommit thecreated_onparameter as it will automatically be set by Django.
post- ForeignKey toPostwith a related name of 'comments'.author_name- CharField with a max length of 64.author_email- EmailField.author_website- URLField that can be blank and null.body- TextField.created_on- DateField withauto_now_addset toTrue.
A simple form used at the bottom of a Post to allow a viewer to submit a comment.
Has the fields author_name, author_email, author_website, and body.
BlogView is a subclass of django.views.generic.list.ListViewwhich diplays allPostobjects that arepublished. It sets the context_object_nameto'posts'so you can refer to{{ posts }}in your template, and uses the standardblog.htmltemplate shipped withdjangorelishblog`.
BlogPostView is a subclass of django.views.generic.base.View which displays a single Post object, a CommentForm and all of the comments related to that Post.