$ mkdir -p /srv/projects/educator_news $ cd /srv/projects/educator_news $ virtualenv venv $ source venv/bin/activate (venv)$ pip install Django psycopg2 South dj-database-url (venv)$ django-admin.py startproject educator_news (venv)$ cd educator_news (venv)educator_news$ python manage.py runserver ... Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C. (venv)educator_news$ cd .. (venv)$ touch .gitignore (venv)$ git add . (venv)$ git commit -am "Initial commit." $ # Create a user for this project. $ sudo -u postgres createuser -P django_user_ednews Enter password for new role: Enter it again: Shall the new role be a superuser? (y/n) n Shall the new role be allowed to create databases? (y/n) y Shall the new role be allowed to create more new roles? (y/n) n $ su postgres Password: $ psql template1 psql (9.1.10) Type "help" for help. template1=# CREATE DATABASE ednews_db OWNER django_user_ednews ENCODING 'UTF8'; CREATE DATABASE template1=# \q $ su ehmatthes Password: $ $ sudo cp /etc/postgresql/9.1/main/pg_hba.conf /etc/postgresql/9.1/main/pg_hba.conf.original $ sudo nano /etc/postgresql/9.1/main/pg_hba.conf # Add the following line, after a similar line. # Don't delete any lines from pg_hba.conf local ednews_db django_user_ednews md5 $ sudo /etc/init.d/postgresql restart import dj_database_url DATABASES = {'default': dj_database_url.config() } DATABASE_URL=postgres://django_user_mysite:password@localhost/mysite_db # Use my env variables export $(cat /srv/projects/mysite/.env) # .gitignore venv/* *.pyc notes/* .env (venv)educator_news$ python manage.py syncdb INSTALLED_APPS = ( 'django.contrib.auth', ... # Utilities/ libraries 'south', ) (venv)educator_news$ python manage.py syncdb (venv)educator_news$ python manage.py startapp ed_news ###highlight=[8,9] from django.conf.urls import patterns, include, url from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', # My urls url(r'^', include('ed_news.urls', namespace='ed_news')), url(r'^admin/', include(admin.site.urls)), ) from django.conf.urls import patterns, url from ed_news import views urlpatterns = patterns('', # My urls # --- Educator News home page --- url(r'^$', views.index, name='index'), ) from django.shortcuts import render_to_response from django.template import RequestContext def index(request): # Static index page for now. return render_to_response('ed_news/index.html', {}, context_instance = RequestContext(request)) (venv)educator_news$ mkdir -p educator_news/static/templates/ed_news (venv)educator_news$ touch educator_news/static/templates/ed_news/index.html Educator News ###highlight=[10,11,12,13] # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True TEMPLATE_DEBUG = True ALLOWED_HOSTS = [] ... DIRNAME = os.path.dirname(__file__) TEMPLATE_DIRS = ( os.path.join(DIRNAME, 'static/templates'), ) # Application definition INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', ... (venv)educator_news$ touch educator_news/educator_news/static/templates/base.html Educator News {% extends base.html %} Educator News Educator News (venv)educator_news$ mkdir educator_news/educator_news/static/css (venv)educator_news$ touch educator_news/educator_news/static/css/site_styles.css ###highlight=[8,9,10,11,12,13,14,15] ... USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.6/howto/static-files/ STATIC_URL = '/static/' STATICFILES_DIRS = ( os.path.join(DIRNAME, 'static'), ) ###highlight=[9,10,11,17,18,19,20] , Educator News {% load staticfiles %}
Educator News
Educator News {% load staticfiles %}
Educator News
#project_title { font-size: 24px; } #login_links { text-align: right; } ###highlight=[20,21,46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,64,65,74,75,76,77,78,79,80,81,82] Static Top Navbar Example for Bootstrap

Navbar example

This example is a quick exercise to illustrate how the default, static and fixed to top navbar work. It includes the responsive CSS and HTML, so it also adapts to your viewport and device.

To see the difference between static and fixed top navbars, just scroll.

View navbar docs »

Static Top Navbar Example for Bootstrap
###highlight=[12,39,45,46] Educator News
###highlight=[14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 64, 65,66,67,68] Educator News {% load staticfiles %}
highlight=[2,3,4,5,6,7,8] from django.db import models from django.contrib.auth.models import User # --- Educator News models --- class UserProfile(models.Model): user = models.OneToOneField(User) ###highlight=[14,15] ... INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', # Utilities/ libraries 'south', # My apps 'ed_news', ) ... ###highlight=[2,6] (venv)educator_news$ python manage.py schemamigration ed_news --initial + Added model ed_news.UserProfile Created 0001_initial.py. You can now apply this migration with: ./manage.py migrate ed_news (venv)educator_news$ python manage.py migrate ed_news educator_news$ python manage.py migrate ed_news Running migrations for ed_news: - Migrating forwards to 0001_initial. > ed_news:0001_initial - Loading initial data for ed_news. Installed 0 object(s) from 0 fixture(s) ###highlight=[3,14,15,16] from django.conf.urls import patterns, include, url from django.core.urlresolvers import reverse from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', # My urls url(r'^', include('ed_news.urls', namespace='ed_news')), url(r'^admin/', include(admin.site.urls)), # Auth urls url(r'^login/', 'django.contrib.auth.views.login', name='login'), url(r'^logout/', 'ed_news.views.logout_view', name='logout_view'), ) {% extends 'base.html' %} {% block content %}

log in:

{% if form.errors %}

Your username and password didn't match. Please try again.

{% endif %}
{% csrf_token %}

{{ form.username.label_tag }}{{ form.username }}

{{ form.password.label_tag }}{{ form.password }}

{% endblock %} ###highlight=[4,5,14,15,16,17,18] from django.shortcuts import render_to_response, redirect from django.template import RequestContext from django.contrib.auth import logout from django.core.urlresolvers import reverse def index(request): # Static index page for now. return render_to_response('ed_news/index.html', {}, context_instance = RequestContext(request)) # Authentication views def logout_view(request): logout(request) # Redirect to home page. return redirect('/') ###highlight=[14,21,22,23,24,25,26]
{% block content %} {% endblock %}
###highlight=[20] {% extends 'base.html' %} {% block content %}

Username: {{ user.username }}

First Name: {{ user.first_name }}

Last Name: {{ user.last_name }}

Email: {{ user.email }}

{% endblock %} ###highlight=[17] from django.conf.urls import patterns, include, url from django.core.urlresolvers import reverse from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', # My urls url(r'^', include('ed_news.urls', namespace='ed_news')), url(r'^admin/', include(admin.site.urls)), # Auth urls url(r'^login/', 'django.contrib.auth.views.login', name='login'), url(r'^logout/', 'ed_news.views.logout_view', name='logout_view'), url(r'^profile/', 'ed_news.views.profile', name='profile'), ) ###highlight=[20,21,22,23] from django.shortcuts import render_to_response, redirect from django.template import RequestContext from django.contrib.auth import logout from django.core.urlresolvers import reverse def index(request): # Static index page for now. return render_to_response('ed_news/index.html', {}, context_instance = RequestContext(request)) # Authentication views def logout_view(request): logout(request) # Redirect to home page. return redirect('/') def profile(request): return render_to_response('registration/profile.html', {}, context_instance = RequestContext(request)) ###highlight=[11] {% extends 'base.html' %} {% block content %}

Username: {{ user.username }}

First Name: {{ user.first_name }}

Last Name: {{ user.last_name }}

Email: {{ user.email }}

Change password

{% endblock %} {% extends 'base.html' %} {% block content %}

Change password:

This page allows you to change your password.

{% if form.errors %}

Please re-enter your passwords. You either entered the wrong current password, or your new passwords did not match.

{% endif %}
{% csrf_token %}

old password:

new password:

new password:

{% endblock %} ###highlight=[6,26,27,28,29,30,31,32,35,36,37,38] from django.shortcuts import render_to_response, redirect from django.template import RequestContext from django.contrib.auth import logout from django.core.urlresolvers import reverse from django.contrib.auth.views import password_change def index(request): # Static index page for now. return render_to_response('ed_news/index.html', {}, context_instance = RequestContext(request)) # Authentication views def logout_view(request): logout(request) # Redirect to home page. return redirect('/') def profile(request): return render_to_response('registration/profile.html', {}, context_instance = RequestContext(request)) def password_change_form(request): if request.method == 'POST': return password_change(request, post_change_redirect='/password_change_successful') else: return render_to_response('registration/password_change_form.html', {}, context_instance = RequestContext(request)) def password_change_successful(request): return render_to_response('registration/password_change_successful.html', {}, context_instance = RequestContext(request)) {% extends 'base.html' %} {% block content %}

Password changed.

Return to profile page.

{% endblock %} ###highlight=[18,19] from django.conf.urls import patterns, include, url from django.core.urlresolvers import reverse from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', # My urls url(r'^', include('ed_news.urls', namespace='ed_news')), url(r'^admin/', include(admin.site.urls)), # Auth urls url(r'^login/', 'django.contrib.auth.views.login', name='login'), url(r'^logout/', 'ed_news.views.logout_view', name='logout_view'), url(r'^profile/', 'ed_news.views.profile', name='profile'), url(r'^password_change/', 'ed_news.views.password_change_form', name='password_change_form'), url(r'^password_change_successful/', 'ed_news.views.password_change_successful', name='password_change_successful'), ) ###highlight=[25] ... ... from django import forms from django.contrib.auth.models import User from ed_news.models import UserProfile class UserForm(forms.ModelForm): password = forms.CharField(widget=forms.PasswordInput()) class Meta: model = User fields = ('username', 'password') class UserProfileForm(forms.ModelForm): class Meta: model = UserProfile fields = () ###highlight=[7,41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84] from django.shortcuts import render_to_response, redirect from django.template import RequestContext from django.contrib.auth import logout from django.core.urlresolvers import reverse from django.contrib.auth.views import password_change from ed_news.forms import UserForm, UserProfileForm def index(request): # Static index page for now. return render_to_response('ed_news/index.html', {}, context_instance = RequestContext(request)) # Authentication views def logout_view(request): logout(request) # Redirect to home page. return redirect('/') def profile(request): return render_to_response('registration/profile.html', {}, context_instance = RequestContext(request)) def password_change_form(request): if request.method == 'POST': return password_change(request, post_change_redirect='/password_change_successful') else: return render_to_response('registration/password_change_form.html', {}, context_instance = RequestContext(request)) def password_change_successful(request): return render_to_response('registration/password_change_successful.html', {}, context_instance = RequestContext(request)) def register(request): # Assume registration won't work. registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): # Save user's form data. user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user profile.save() # Registration was successful. registered = True # Not requiring email validation yet, so log user in. #user = authenticate(username=user.username, password=user.password) #login(request, user) print 'un, pw', user.username, user.password else: # Invalid form/s. # Print errors to console; should log these? print 'ufe', user_form.errors print 'pfe', profile_form.errors else: # Send blank forms. user_form = UserForm() profile_form = UserProfileForm() return render_to_response('registration/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered, }, context_instance = RequestContext(request)) {% extends 'base.html' %} {% block content %}

Register:

This page allows you to register a new account.

{% if registered %}

Thank you for registering!

Return to the homepage.

{% else %}
{% csrf_token %} {{ user_form.as_p }} {{ profile_form.as_p }}

{% endif %} {% endblock %} ###highlight=[20] from django.conf.urls import patterns, include, url from django.core.urlresolvers import reverse from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', # My urls url(r'^', include('ed_news.urls', namespace='ed_news')), url(r'^admin/', include(admin.site.urls)), # Auth urls url(r'^login/', 'django.contrib.auth.views.login', name='login'), url(r'^logout/', 'ed_news.views.logout_view', name='logout_view'), url(r'^profile/', 'ed_news.views.profile', name='profile'), url(r'^password_change/', 'ed_news.views.password_change_form', name='password_change_form'), url(r'^password_change_successful/', 'ed_news.views.password_change_successful', name='password_change_successful'), url(r'^register/', 'ed_news.views.register', name='register'), ) ###highlight=[3,5] from django.contrib import admin from ed_news.models import UserProfile admin.site.register(UserProfile) ... class Submission(models.Model): """An abstract class for the two types of submission, which are an article and a text submission. Text submissions can be questions, ie Ask EN, or posts such as Show EN. """ # Stick with programming 80-char limit for now. # It's what HN uses, which fits nicely on mobile. title = models.CharField(max_length=80) author = models.ForeignKey(User) upvotes = models.IntegerField(default=0) downvotes = models.IntegerField(default=0) points = models.IntegerField(default=0) submission_time = models.DateTimeField(auto_now_add=True) class Meta: abstract = True def __unicode__(self): return self.title class Article(Submission): url = models.URLField() (venv)educator_news$ python manage.py schemamigration --auto ed_news (venv)educator_news$ python manage.py migrate ed_news ###highlight=[16] ###highlight=[11] from django.conf.urls import patterns, url from ed_news import views urlpatterns = patterns('', # My urls # --- Educator News home page --- url(r'^$', views.index, name='index'), url(r'^submit/', views.submit, name='submit'), ) ###highlight=[5,19,20,21,22] from django import forms from django.contrib.auth.models import User from ed_news.models import UserProfile from ed_news.models import Article class UserForm(forms.ModelForm): password = forms.CharField(widget=forms.PasswordInput()) class Meta: model = User fields = ('username', 'password') class UserProfileForm(forms.ModelForm): class Meta: model = UserProfile fields = () class ArticleForm(forms.ModelForm): class Meta: model = Article fields = ('title', 'url',) ###highlight=[9,13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46] from django.shortcuts import render_to_response, redirect from django.template import RequestContext from django.contrib.auth import logout from django.core.urlresolvers import reverse from django.contrib.auth.views import password_change from ed_news.forms import UserForm, UserProfileForm from ed_news.forms import ArticleForm ... ... # --- Educator News views --- def submit(request): submission_accepted = False if request.method == 'POST': article_form = ArticleForm(data=request.POST) if article_form.is_valid(): print 'af', article_form print 'afcd', article_form.cleaned_data print 'u', request.user print 'uid', request.user.id print 'utype', type(request.user) print article_form.cleaned_data['url'] article = article_form.save(commit=False) article.author = request.user article.save() submission_accepted = True else: # Invalid form/s. # Print errors to console; should log these? print 'ae', article_form.errors else: # Send blank forms. article_form = ArticleForm() return render_to_response('ed_news/submit.html', {'article_form': article_form, 'submission_accepted': submission_accepted, }, context_instance = RequestContext(request)) {% extends 'base.html' %} {% block content %}

Submit

You can use this form to submit an article.

{% if user.is_authenticated and submission_accepted %}

Thank you for your submission!

{% elif user.is_authenticated %}

Submit form

{% csrf_token %} {{ article_form.as_p }}

{% else %}

You have to log in or register if you would like to make a submission.

{% endif %} {% endblock %}