CMS Project in django

hai friends....


Project is CMS:Cateen Management System
===============================
in the terminal to use virtual environment
type



$ sudo apt-get install python-virutalenv
$mkdir cms
$virtualenv ./cms
$cd cms

cms$ls
bin  include  lib  local

\cms$ls bin
activate      activate.fish     easy_install      pip   pip2.7  python2
activate.csh  activate_this.py  easy_install-2.7  pip2  python  python2.7

cms$source bin/activate
(cms)cms$


to install django 1.5 create requirements.txt and write Django==1.5 and save

in the terminal run
$ pip install -r requirements.txt

creating login and registration for project...






$django-admin.py startproject cateen_management_system
$cd canteen_management_system
$ python manage.py startapp login




in the setting.py 

change 

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME':'loginapp.sqlite',                      # Or path to database file if using sqlite3.
        # The following settings are not used with sqlite3:
        'USER': '',
        'PASSWORD': '',
        'HOST': '',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
        'PORT': '',                      # Set to empty string for default.
    }
}


$ python manage.py syncdb
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table django_site

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'erp'): erp
Email address: erp@gmail.com
Password: 
Password (again): 
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)


in setting.py
TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
      os.path.join(os.path.dirname(__file__), 'templates'),

)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
    'login',
)


add import os in setting.py...






And also add following lines into your settings.py file. The utility of this two lines is if a user want to access
a method that that requires login and the user is not logged in then it will redirect the user to login page.
1
2
import django.contrib.auth
django.contrib.auth.LOGIN_URL = '/'
Now create a file called forms.py inside login folder. And paste the folowing code into that file.
This code is used to create the registration form.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#files.py
import re
from django import forms
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
class RegistrationForm(forms.Form):
    username = forms.RegexField(regex=r'^\w+$', widget=forms.TextInput(attrs=dict(required=True, max_length=30)), label=_("Username"), error_messages={ 'invalid': _("This value must contain only letters, numbers and underscores.") })
    email = forms.EmailField(widget=forms.TextInput(attrs=dict(required=True, max_length=30)), label=_("Email address"))
    password1 = forms.CharField(widget=forms.PasswordInput(attrs=dict(required=True, max_length=30, render_value=False)), label=_("Password"))
    password2 = forms.CharField(widget=forms.PasswordInput(attrs=dict(required=True, max_length=30, render_value=False)), label=_("Password (again)"))
    def clean_username(self):
        try:
            user = User.objects.get(username__iexact=self.cleaned_data['username'])
        except User.DoesNotExist:
            return self.cleaned_data['username']
        raise forms.ValidationError(_("The username already exists. Please try another one."))
    def clean(self):
        if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
            if self.cleaned_data['password1'] != self.cleaned_data['password2']:
                raise forms.ValidationError(_("The two password fields did not match."))
        return self.cleaned_data
Next open the views.py file and paste the following code into there.
1
2
3
4
5
6
7
8
9
10
11
12
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
#views.py
from login.forms import *
from django.contrib.auth.decorators import login_required
from django.contrib.auth import logout
from django.views.decorators.csrf import csrf_protect
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import RequestContext
@csrf_protect
def register(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            user = User.objects.create_user(
            username=form.cleaned_data['username'],
            password=form.cleaned_data['password1'],
            email=form.cleaned_data['email']
            )
            return HttpResponseRedirect('/register/success/')
    else:
        form = RegistrationForm()
    variables = RequestContext(request, {
    'form': form
    })
    return render_to_response(
    'registration/register.html',
    variables,
    )
def register_success(request):
    return render_to_response(
    'registration/success.html',
    )
def logout_page(request):
    logout(request)
    return HttpResponseRedirect('/')
@login_required
def home(request):
    return render_to_response(
    'home.html',
    { 'user': request.user }
    )
Now create a folder called templates inside login folder.
Next create a file called base.html inside templates folder and paste the following code in that file.
1
2
3
4
5
6
7
8
9
10
11
12
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <title>Django Login Application |
        {% block title %}{% endblock %}</title>
    </head>
    <body>
        <h1>{% block head %}{% endblock %}</h1>
        {% block content %}{% endblock %}
    </body>
</html>
Aslo create a file called home.html inside templates folder and paste the following code in that file.
1
2
3
4
5
6
7
{% extends "base.html" %}
{% block title %}Welcome to Django{% endblock %}
{% block head %}Welcome to Django{% endblock %}
{% block content %}
    <p>Welcome {{ user.username }} !!!</p>
    <a href="/logout/">Logout</a>
{% endblock %}
Next create a folder called registration inside templates folder.
Create a file called login.html inside the registration folder and paste the following code in that file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{% extends "base.html" %}
{% block title %}Login{% endblock %}
{% block head %}Login{% endblock %}
{% block content %}
    {% if form.errors %}
        <p>Your username and password didn't match. Please try again.</p>
    {% endif %}
    <form method="post" action=".">{% csrf_token %}
        <table border="0">
            <tr><th><label for="id_username">Username:</label></th><td>{{ form.username }}</td></tr>
            <tr><th><label for="id_password">Password:</label></th><td>{{ form.password }}</td></tr>
        </table>
        <input type="submit" value="Login" />
        <input type="hidden" name="next" value="/home" />
    </form>
    <a href="/register">Register</a>
{% endblock %}
Next create a file called register.html inside the registration folder and paste the following code in that file.
1
2
3
4
5
6
7
8
9
10
11
12
{% extends "base.html" %}
{% block title %}User Registration{% endblock %}
{% block head %}User Registration{% endblock %}
{% block content %}
    <form method="post" action=".">{% csrf_token %}
        <table border="0">
            {{ form.as_table }}
        </table>
        <input type="submit" value="Register" />
    </form>
    <a href="/">Login</a>
{% endblock %}
Next create a file called success.html inside the registration folder and paste the following code in that file.
1
2
3
4
5
6
7
8
9
{% extends "base.html" %}
{% block title %}Registration Successful{% endblock %}
{% block head %}
 Registration Completed Successfully
{% endblock %}
{% block content %}
    Thank you for registering.
    <a href="/">Login</a>
{% endblock %}
Finally you need to modify the urls.py file. So open that file and paste the following code in that file.
1
2
3
4
5
6
7
8
9
10
11
from django.conf.urls import patterns, include, url
from login.views import *
urlpatterns = patterns('',
    url(r'^$', 'django.contrib.auth.views.login'),
    url(r'^logout/$', logout_page),
    url(r'^accounts/login/$', 'django.contrib.auth.views.login'), # If user is not login it will redirect to login page
    url(r'^register/$', register),
    url(r'^register/success/$', register_success),
    url(r'^home/$', home),
)
Now to run th app go to the terminal and enter the following commnad.
1
python manage.py runserver
And to view the application open a browser and go to the following url
reference document link


https://mayukhsaha.wordpress.com/2013/05/09/simple-login-and-user-registration-application-using-django/



after registering from the page .... if you want to check in the databse


type


$sudo apt-get install sqlite3
m$ sqlite3 loginapp.sqlite
SQLite version 3.8.2 2013-12-06 14:53:30
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .table
auth_group                  auth_user_user_permissions
auth_group_permissions      django_content_type       
auth_permission             django_session            
auth_user                   django_site               
auth_user_groups  


sqlite> .schema auth_user
CREATE TABLE "auth_user" (
    "id" integer NOT NULL PRIMARY KEY,
    "password" varchar(128) NOT NULL,
    "last_login" datetime NOT NULL,
    "is_superuser" bool NOT NULL,
    "username" varchar(30) NOT NULL UNIQUE,
    "first_name" varchar(30) NOT NULL,
    "last_name" varchar(30) NOT NULL,
    "email" varchar(75) NOT NULL,
    "is_staff" bool NOT NULL,
    "is_active" bool NOT NULL,
    "date_joined" datetime NOT NULL
);



sqlite> select * from auth_user;
1|pbkdf2_sha256$10000$zXsgRckHdu5Q$o+BWhEKyL/SD+AQjR7LxCxTqwa+NyUVYgJgv8sKapAk=|2015-02-27 12:49:09.271758|1|erp|||erp@gmail.com|1|1|2015-02-27 12:49:09.271758
2|pbkdf2_sha256$10000$0mpxZVmHpFWw$WrXiRQHrdYYsMVvJThwlugSF+M1tqvuHYmN4WbvCBmU=|2015-02-27 13:10:17.519584|0|deepak|||just2deepu@gmail.com|0|1|2015-02-27 13:05:52.527986

https://www.youtube.com/watch?v=VY-3hP8wlOc


installing mysql for ubuntu



No comments:

Post a Comment