2
votes

I am new to django web programming and struggling from 1 month to get the hang of view + models + forms + templates... and i just cant get it fully. please can anyone explain it in simple and to the point. thanks for your help.

According to me if i need to show a login page I have 2 options. 1 to use build-in UserCreadentialForms way which is in all the youtube tutorials. 2 is to use custom built. i have successfully used 1 way and now tring to use custom built forms. for this, i goes to models and create a model of my choice (given below) then goes to run that migrate commands to actually create them in database... now tell me how to show/ fillout/ render those fields in the templates. (i am currently using admin url to register/fill out the data in fields and display them on template)

base template
<!DOCTYPE html>

<html lang="en">
<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>

<body>
    {% if title %}
        <title>Django Blog - {{ title }}</title>
    {% else %}
        <title>Django Blog</title>
    {% endif %}

    <h1>I am base Template</h1>

    <div class="container">
        {% block content %} {% endblock %}
    </div>


    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

</body>
</html>
 child html
    {% extends "app1/base.html" %}

    {% block content %}
        <h1>Enter data</h1>
        {% for d in data %}

            <h5>Username: <h7>{{ d.username }}</h7></h5>
    <br>
            <h5>Email:  <h7>{{ d.email }}</h7></h5>
    <br>
            <h5>Logtime: <h7>{{ d.logtime }}</h7></h5>



        {% endfor %}
    {% endblock content %}

'''

models.py from django.db import models

Create your models here. class loginmodel(models.Model):

username = models.CharField(max_length=30)
email = models.EmailField(max_length=30)
logtime = models.DateTimeField(auto_now_add=True)

def __str__(self):
    return self.username

'''

'''

urls from django.urls import path from . import views

urlpatterns = [ path('', views.index, name='index'), path('page1', views.page1, name='page1'), path('dashboard/', views.dashboard, name='dashboard'), path('about/', views.about, name='about'), path('base/', views.base, name='base'), ]

'''

'''

views.py from django.shortcuts import render from .models import loginmodel from django.http import HttpResponse

data = [ { 'username': 'Haider', 'email': '[email protected]',

},
{
    'username': 'ghufran',
    'email': '[email protected]',

} ]

def index(request): return HttpResponse("Home Page app1.views.")

def page1(request): return HttpResponse("page1 app1.views ")

def dashboard(request): context = { 'data': loginmodel.objects.all() } return render(request, 'app1/print.html', context)

def base(request): return render(request, 'app1/base.html')

def about(request): return render(request, 'app1/about.html')

def loign(request): log =

'''

1

1 Answers

4
votes

There is a lot to tell but to cut everything in short,

Views - It contains your logic function, all the main function goes to views.py of the app

Models - It contains the schema of your database that is the overall structuring of the database.

Template - It contains your frontend content, basically the html part that we are all familiar with.

Forms - Now Forms is something that can be done using django or without it also but django_form make thing easy so Form have elements like fields as usual which is associated with a Model. Django Form make thing ridiculously easy and fast.

enter image description here

Hope this helps!