Django Hello World

First install python 3.5.3 and Django.
First instll pip
01.Get get-pip.py

02.
python get-pip.py
03.
pip install virtualenvwrapper-win
04.
mkvirtualenv myproject
05.
workon myproject
06.
pip install django
07.
django-admin –version
08.
django-admin startproject mysite
09.
cd mysite
python manage.py startapp polls

Your apps can live anywhere on your Python path.
In this tutorial, we’ll create our poll app right next to your manage.py file so
that it can be imported as its own top-level module, rather than a submodule of mysite.
10.

install from here

reference

pip install Django
python manage.py startapp myapp

INSTALLED_APPS = (
‘django.contrib.admin’,
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.messages’,
‘django.contrib.staticfiles’,
‘myapp’,
)

in view

from django.http import HttpResponse

def hello(request):
text = “””welcome to my app !”””
return HttpResponse(text)

urls.py

from django.conf.urls import include, url
from django.contrib import admin
from myapp.views import hello
urlpatterns = [
#url(r’^admin/’, admin.site.urls),
url(r’^hello/$’, hello),
]

Create a Model in app/Models

from django.utils import timezone

class Post(models.Model):

title = models.CharField(max_length=200)
text = models.TextField()
created_date = models.DateTimeField(
default=timezone.now)
published_date = models.DateTimeField(
blank=True, null=True)

def publish(self):
self.published_date = timezone.now()
self.save()

def __str__(self):
return self.title

1.python manage.py makemigrations blog
2.python manage.py migrate blog
python manage.py migrate #allmigration

#Create
dreamreal = Dreamreal(
website = “www.polo.com”, mail = “sorex@polo.com”,
name = “sorex”, phonenumber = “002376970”
)

dreamreal.save()

#Read ALL entries
objects = Dreamreal.objects.all()
res =’Printing all Dreamreal entries in the DB :

for elt in objects:
res += elt.name+”

#Read a specific entry:
sorex = Dreamreal.objects.get(name = “sorex”)
res += ‘Printing One entry

res += sorex.name

#Delete an entry
res += ‘
Deleting an entry

sorex.delete()

#Update
dreamreal = Dreamreal(
website = “www.polo.com”, mail = “sorex@polo.com”,
name = “sorex”, phonenumber = “002376970”
)

dreamreal.save()
res += ‘Updating entry

dreamreal = Dreamreal.objects.get(name = ‘sorex’)
dreamreal.name = ‘thierry’
dreamreal.save()

More info

Leave a Reply

Your email address will not be published. Required fields are marked *