التخطي إلى المحتوى الرئيسي

المشاركات

عرض الرسائل ذات التصنيف Django Tutorials

Django User Authentication to manage registration,login and logout operation | #django

 Django provide user authentication system to manage secure Login, Registration and Logout. Code of views.py from django.shortcuts import render,redirect from django.http import HttpResponse from . models import Register from django.contrib.auth.models import User from django.contrib.auth import authenticate from django.contrib.auth import logout,login def reg ( request ):     if request.method== 'POST' :                 user = User.objects.create_user(request.POST[ 'txtuser' ], request.POST[ 'txtemail' ],request.POST[ 'txtpass' ])         user.first_name=request.POST[ 'txtfirst' ]         user.last_name=request.POST[ 'txtlast' ]         user.save()         return render(request, "helloapp/reg.html" , { "key" : "data inserted successfully" })     else :         return render(reques...

Django Introduction & Installation in Python

It is a Web-based Framework of Python which provides MVT(Model, View, Template) Design pattern to create a Dynamic web application. Django MVC is also known as MVT architecture. MVT means Model, View, and Template,   Model is used to provide a data access layer, View is used to provide business access layer or Code layer,  and Template is used to provide user access layer or design. Why Django is best as compare to other frameworks:- 1) Django contains predefined libraries to create secure, reliable, and Fast dynamic web applications. 2) Django Framework is Open Source so that we can easily customize the Django Framework program code and structure. 3) Django provides rapid application development tools because it has an inbuilt app to create a login , admin dashboard, user management (ACL access control list ),  Session management, etc. 4) Django Framework can easily be integrated with HTML, CSS, JS, and Jquery to design web pages. 5) Django Framework ...

Django Project Flow, Django Project Architecture, How Django Framework Works Internally

Project --------> Project URL-----> App URL ------> View           Method 1) When we run the Django project then the first projec t urls.py will be loaded 2) Project urls.py file contains a collection of app URLs means if we create 5 different apps then five different app URLs will be declared here 3) app urls.py file contain the path of the method which will be defined under view What is the difference between project and app:-  A project is a collection of the app for example if we create a project for a feedback system then the student app, faculty app, admin app will be different components of the project. app contain views.py ,templates for html file,css file,image,js,jquery,model e tc means app is the complete component of project.                                           

Create NEW APP WITH FORM ELEMENT in DJANGO

1) Create New App For Si       python manage.py startapp siapp 2)  Create urls.py under app:- from django.urls import path from . import views urlpatterns = [     path('', views.index, name='index'),  ] 3) Create templates folder under app and subfolder using  appname and create .html     design form element using Html    <form action="silogic"  method="get">     ...   </form>   4)  edit app url and define logic method     from django.urls import path from . import views urlpatterns = [     path('', views.index, name='index'),      path('silogic', views.silogic, name='silogic'),  ] 5  define views.py two method first for load and second for action      def index(request):        return render(request,"appname/htmlfilename")    def silogic(reque...

How to create single method to load template and write code?

  Django provides a single view method to render the template using the GET method and POST method to write code after form submission. It is the standard approach to write Django Code. Syntax to write load template and logic using a single file. def Methodname(request):          if request.method=="POST":                  Write Business Code                   return render(request,"appname/filename",{'key':output})       return render(request,"appname/filename") Now I am explaining the example of the SI Calculation App? Step1st:-   Create APP using   python manage.py startapp siapp Step2nd:-   go into settings.py and enter appname Step3rd:-  go to project urls.py and create app urls step4th:-  create urls.py under app and define method urls from django.urls import path from . import views urlpatterns=[ path('...

What is Difference between GET Method and Post Method in Django Form?

1)  GET METHOD:-    It is used to submit the data of form elements into a web server using URL parameters, GET method call the HttpGet object to send data from form to web server. all data will be shown on the  URL hence get is not secure and we can send limited data using GET Method. default URL method is GET means when we hit any web URL under web browser then it will be called using the GET method. 2)  POST Method:-  It is used to submit the data of form elements into a web server using the HttpPost object.it sends data internally then data will not be shown on URL parameters hence it provides better security as compared to the get method, we can send unlimited data using the Post method hence we often prefer the post method to send data from client machine to server machine. Django uses csrf_token to send data using the POST method csrf_token will generate a token key from the browser to match from the server. CSRF means   cross-site reque...

How to create form using POST Method

POST is the most secure and reliable method to send data from client machines to server machines hence we should always use the POST method to create form elements. Django provides the CSRF option to send data using the POST method. CSRF means Cross-site request forgery . it will protect invalid requests under the webserver to check the security token. If the security token is valid then the server will process requests otherwise deny the request. Now we are creating a calculation of simple interest using the POST method. Step1st:-   Create an app if not exist, create urls.py to define method if not created. urls.py from django.urls import path from . import views urlpatterns=[   path('',views.index,name='index'),   path('home',views.index,name='index')   ] Step2nd:-    Create templates/appname/filename to design template create index.html <!DOCTYPE html> <html> <head> <title></title> <link href="/static/style.css...

How to create from in Django application using GET Method

To take input from user's we will create HTML Form Template using Html Form Elements and attribute. Step to create the form under Django framework:- 1) Create " templates " Directory Under App and under "templates" Create app name directory then create a file and design form according to the requirement templates/appname/filename for example, siapp is the appname then we create templates folder then create siapp folder then create filename 2) Render Html page under view load method def index(request):    #load method return render(request,"appname/htmlfilename") 3)   Create Action Method in views.py      def silogic(request):  #action method logic return data 4) define the load method, action method  url under urls.py , and write action method URL under form action attribute .      <form action="actionurl" method="get">     </form> for example, action method is silogic() th...

Complete Html Form Elements Tutorials in Django Application

In this article, we  define complete HTML elements using Textbox, Radiobutton, Checkbox, Listbox, Textarea, etc To run this article code, you should have basic knowledge of HTML then you can click on the below link where I have to write articles of HTML code. Click to learn HTML 1)  Code of Design File:- reg.html:- <!DOCTYPE html> <html> <head> <title></title> </head> <body> <form action="reglogic" method="post"> {% csrf_token  %} <input type="radio" name="course" value="PYTHON" />PYTHON<input type="radio" name="course" value="PHP" />PHP<br><br> <input type="checkbox" name="c[]" value="C" />C<input type="checkbox" name="c[]" value="CPP" />CPP<input type="checkbox" name="c[]" value="DS" />DS<br><br> <...

Create Form using Post Method in Django With Form- Validation

Create Form using Post Method in Django with  Form-validation:- Post provides better security as compare to get because it will transfer data internally using CSRF (Cross-site request forgery) it will protect an application from cross-site scripting attack from the malicious site. We will use method="post" attribute under the form tag in HTML Form and request.POST to get data of form element in views.py. Example  of Post Method Create views.py using two different methods first is the load method to load the HTML page and second is the action method to implement logic. from django.shortcuts import render from django.http import HttpResponse def index(request):    #load method return render(request,"siapp/silogic.html") def silogic(request):  #action method p=request.POST["txtnum1"] r=request.POST["txtnum2"] t=request.POST["txtnum3"] si=(float(p)*float(r)*float(t))/100 return render(request,"siapp/silogic...

CheckBox Tutorials in Django

The checkbox is used to check multiple options in the Django form. We will use the Checkbox HTML element to design CheckBox. Syntax of Checkbox for Django Form. <input type="checkbox" name="chk[]"  value="content"    />Content <input type="checkbox" name="chk[]"  value="content"   />Content here chk[] is the list that contains multiple values. We will use request.POST.getlist() to get the element of Checkbox and Listbox. Step to implement Checkbox on Django Form using Single Method on Views.py :- 1) Create urls.py under app:- from django.urls import path from. import views urlpatterns = [          path('checkbox', views.checkbox, name='checkbox'),    ] 2) Create templates/appname/checkbox.html <!DOCTYPE html> <html> <head> <title></title> </head> <body> <form action="" method="post"> {% csrf_token %} <input type="checkbox...

ListBox and CheckBox combine tutorials in Django,Django Form tutorial,Django ListBox,Django CheckBox

Listbox is used to select multiple items and Checkbox also used to check multiple items. When elements are more then we prefer Listbox when an element is less then we prefer Checkbox to create form elements.. I am providing complete code to implement ListBox and CheckBox in Django Code for HTML Template <form method="post" action="add/addlogic"> {% csrf_token %} <input type="checkbox" name="checks[]" value="C" />C <input type="checkbox" name="checks[]" value="CPP" />CPP <input type="checkbox" name="checks[]" value="DS" />DS <input type="checkbox" name="checks[]" value="ETC" />ETC <br><br> <select name="course[]" multiple="true"> <option value="JAVA">JAVA</option>     <option value=".NET">.NET</option>     <option value...

Django addition, multiplication, substraction and division using multiple button's

views.py code from django.shortcuts import render def index(request): if request.method=='POST'  p = request.POST["txtp"] r = request.POST["txtr"] if request.POST['btnsubmit']=='+': c=int(p)+int(r) elif request.POST['btnsubmit']=='-': c=int(p)-int(r) elif request.POST['btnsubmit']=='*': c=int(p)*int(r) return render(request,"siapp/index.html",{'res':c}) return render(request,"siapp/index.html") code of index.html <!DOCTYPE html> <html> <head> <title></title> <link href="/static/style.css"  type="text/css" rel="stylesheet" /> </head> <body> <header> <center><h1>SI FORM </h1></center> </header> <section> <div style="padding-top:50px;padding-left: 200px; ">   <form action="" me...

What is extends in Django? How to create standard template design in Django

  It is used to provide the extendibility of parent template or base template to child template. If we want to create a master-child template in the Django application then we use extends operation in Django. Child pages inherit the complete functionality of the base template. Syntax of base template or master template <html> <head> </head> <body> <div id="container"> <header> </header> <section> {% block blockame %}     Content Place Holder   {% endblock %} </section> <footer> </footer> </div> </body> </html> Syntax of child page:- {% extends 'appname/parentpage.html' %} {% block blockname %}     hello {% endblock %} Now I am providing the code of the base.html or master.html file:- <!DOCTYPE html> <html> <head> <title></title> <link rel="stylesheet" type="text/css" href="/static/style.css"> </head> <body...