Table of Contents
VFOSS News App
Getting Start
Useful things
Follow this book:
Django 1.7 1.9
Related project:
- multilingual based on dragoman: https://github.com/bitlabstudio/django-multilingual-news/blob/master/multilingual_news/models.py
Create a Hello World App
Create app
python manage.py startapp vfoss_news
Create a view handler
1 from django.shortcuts import render
2 from django.http import HttpResponse
3
4 def index(request):
5 return HttpResponse("VFOSS news says hey there world!")
Link app urls with the project's urls.
Note: django-cms urls must be placed last in the list. Otherwise it will cause 404 error.
9 urlpatterns = i18n_patterns('', 10 url(r'^admin/', include(admin.site.urls)), 11 # python-social-auth 12 url('', include('django.contrib.auth.urls', namespace='auth')), 13 url('', include('social.apps.django_app.urls', namespace='social')), 14 # djangocms-blog 15 url(r'^taggit_autosuggest/', include('taggit_autosuggest.urls')), 16 # vfoss-news 17 url(r'^news/', include('vfoss_news.urls')), 18 # django-cms 19 url(r'^', include('cms.urls')), 20 ) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Adding template
mkdir -p templates/vfoss_news cd templates/vfoss_news vim index.html
1 <!DOCTYPE html> 2 <html> 3 4 <head> 5 <title>Rango</title> 6 </head> 7 8 <body> 9 <h1>Rango says...</h1> 10 hello world! <strong>{{ boldmessage }}</strong><br /> 11 <a href="/rango/about/">About</a><br /> 12 </body> 13 14 </html>
Update views.py
1 from django.shortcuts import render 2 from django.http import HttpResponse 3 from django.shortcuts import render 4 5 def index(request): 6 7 # Construct a dictionary to pass to the template engine as its context. 8 # Note the key boldmessage is the same as {{ boldmessage }} in the template! 9 context_dict = {'boldmessage': "I am bold font from the context"} 10 11 # Return a rendered response to send to the client. 12 # We make use of the shortcut function to make our lives easier. 13 # Note that the first parameter is the template we wish to use. 14 15 return render(request, 'vfoss_news/index.html', context_dict) 16
Create Model
class Category(models.Model): name = models.CharField(max_length=128, unique=True) def __unicode__(self): #For Python 2, use __str__ on Python 3 return self.name class Page(models.Model): category = models.ForeignKey(Category) title = models.CharField(max_length=128) url = models.URLField() views = models.IntegerField(default=0) def __unicode__(self): #For Python 2, use __str__ on Python 3 return self.title
Migrations
python manage.py makemigrations vfoss_news python mamage.py migrate
Whenever you add to existing models, you will have to repeat this process running python manage.py makemigrations <app_name>, and then python manage.py migrate
Django Admin
1 from django.contrib import admin 2 from vfoss_news.models import Category, News 3 4 # Register your models here. 5 6 admin.site.register(Category) 7 admin.site.register(News)
Model - Template - View
Basic Workflow: Data Driven Pages
There are five main steps that you must undertake to create a data driven webpage in Django.
- First, import the models you wish to use into your application’s views.py file.
- Within the view you wish to use, query the model to get the data you want to present.
- Pass the results from your model into the template’s context.
- Setup your template to present the data to the user in whatever way you wish.
- If you have not done so already, map a URL to your view.
Import Model
Update Index views.py
Detail views
Update Category Table with Slug Field
Add slug fied to models.py:
from django.template.defaultfilters import slugify class Category(models.Model): name = models.CharField(max_length=128, unique=True) views = models.IntegerField(default=0) likes = models.IntegerField(default=0) slug = models.SlugField(unique=True) def save(self, *args, **kwargs): self.slug = slugify(self.name) super(Category, self).save(*args, **kwargs) def __unicode__(self): return self.name
./manage makemigrations vfoss_news ./manage migrate
Auto populate slug field. Add following to admin.py
from django.contrib import admin from rango.models import Category, Page # Add in this class to customized the Admin Interface class CategoryAdmin(admin.ModelAdmin): prepopulated_fields = {'slug':('name',)} # Update the registeration to include this customised interface admin.site.register(Category, CategoryAdmin) admin.site.register(Page)
Form
continue here: http://www.tangowithdjango.com/book17/chapters/forms.html