Django Tutorial With Template

Today I have learn how to work with Templates in django.

Now start  the work on templates first set is create static pages in Django.

1) open your settings file (settings.py)

2)Find this line in settings.py if this exits then is ok otherwise add this in settings.py file STATIC_URL = ‘/static/’

3) Add this settings for template files

TEMPLATE_DIRS =(
 os.path.join(os.path.dirname(BASE_DIR), "/home/sofat/static", "mytemplate"),
)

4)Now create static directory and inner of this create mytemplate directory

5) Now open your urls.py file and set your Static page as HomePage.


from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = patterns('',
# Examples:
url(r'^$', 'signUp.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),

url(r'^admin/', include(admin.site.urls)),
)

6) Open you view file to connect your view with your static page

from django.shortcuts import render, render_to_response, RequestContext, HttpResponseRedirect
from django.contrib import messages
from .forms import SignUpForm
def home(request):

form = SignUpForm(request.POST or None)
if form.is_valid():
save_it = form.save(commit=False)
save_it.save()
messages.success(request, 'Thanks for Register')
return HttpResponseRedirect('/thank-you/')

return render_to_response("signup.html",locals(),context_instance = RequestContext(request))

def thankyou(request):
return render_to_response("thankyou.html",locals(),context_instance = RequestContext(request))

7)Now create new html page and save in static/mytemplate

8) Run your code

python manage.py runserver

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s