article

Monday, April 6, 2020

Django basic Model View Template and static files handling


Django basic Model View Template and static files handling

Install Django Version 3.0.2 on windows 10 | Create Project and Setting Admin


Models.py


from django.db import models
from django.utils import timezone
from django.conf import settings

# Create your models here.

class Contact(models.Model):
    firstName = models.CharField("First name", max_length=255, blank = True, null = True)
    lastName = models.CharField("Last name", max_length=255, blank = True, null = True)
    email = models.EmailField()
    phone = models.CharField(max_length=20, blank = True, null = True)
    address = models.TextField(blank=True, null=True)
    description = models.TextField(blank=True, null=True)
    createdAt = models.DateTimeField("Created At", auto_now_add=True)

    def __str__(self):
        return self.firstName
  
class Post(models.Model):
    author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) #models.ForeignKey – this is a link to another model.
    title = models.CharField(max_length=200)
    text = models.TextField() #models.TextField – this is for long text without a limit. Sounds ideal for blog post content, right?
    created_date = models.DateTimeField(default=timezone.now) #models.DateTimeField – this is a date and time.
    published_date = models.DateTimeField(blank=True, null=True)

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

    def __str__(self):
        return self.title
Settings.py
 
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

C:\my_project_django\devproject>python manage.py makemigrations myapp
C:\my_project_django\devproject>python manage.py migrate


Views.py
 
from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse  
import datetime 
from django.template import loader 

def index(request):  
   template = loader.get_template('index.html') # getting our template  
   name = {  #Variable Example
       'student':'Cairocoders Ednalan'  
   } 
   return HttpResponse(template.render(name))       # rendering the template in HttpResponse  
   
#def index(request):  
#    now = datetime.datetime.now()   
#    html = "<html><body><h3>Now time is %s.</h3></body></html>" % now  
#    return HttpResponse(html)    # rendering the template in HttpResponse   

C:\my_project_django\devproject>python manage.py runserver

urls.py
from django.contrib import admin
from django.urls import path
from myapp import views  

urlpatterns = [
    path('admin/', admin.site.urls),
    path('index/', views.index),    
]
templates/index.html
//index.html
<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <title>Index</title>  
 {% load static %} 
 <script src="{% static '/js/script.js' %}" type="text/javascript"></script> 
    <link href="{% static 'css/style.css' %}" rel="stylesheet">  
</head>  
<body>  
<h1>Welcome to Django!!!</h1>  
<h3>My Name is: {{ student }}</h3>  
<img src="{% static '/test.jpg' %}"/>  
</body>  
</html>

static/js/script.js
alert('test javascript');

static/css/style.css
h1{ color:red;font-size:35px; }

Related Post