django python Archives - Tech Insights Unveiling Tomorrow's Tech Today, Where Innovation Meets Insight Fri, 05 Nov 2021 07:01:23 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.2 https://i0.wp.com/reactconf.org/wp-content/uploads/2023/11/cropped-reactconf.png?fit=32%2C32&ssl=1 django python Archives - Tech Insights 32 32 230003556 How to Create Cascading/Dependent Dropdown List in Django Python https://reactconf.org/how-to-create-cascading-dependent-dropdown-list-in-django-python/ https://reactconf.org/how-to-create-cascading-dependent-dropdown-list-in-django-python/#comments Fri, 05 Nov 2021 07:01:23 +0000 https://labpys.com/?p=397 In this tutorial, we will learn How to Create a Cascading/Dependent Dropdown List in Django Python. In this small application, we are going to improve user experience by Creating a …

The post How to Create Cascading/Dependent Dropdown List in Django Python appeared first on Tech Insights.

]]>

In this tutorial, we will learn How to Create a Cascading/Dependent Dropdown List in Django Python. In this small application, we are going to improve user experience by Creating a dynamic Django dropdown list using jQuery.

Create a Django Dropdown project 

                We need to create a Django project  dropdownfill

               Django-admin startproject dropdownfill

Create Django App

                Now then need to create an app dropdown to perform the Cascading/Dependent Dropdown List in Django Python.

                 python  manage.py startapp dropdownfill

Configure  Application Settings

                Configure application settings by registering the App_name into the settings.py in a specific location at INSTALLED_APPS

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'dropdown',
]

Create the Model

Now we need to create models.py .

from django.db import models
from django.db.models.base import Model
from django.db.models.deletion import CASCADE

# Create your models here.


class department(models.Model):
    departmentname = models.CharField(max_length=100)


class employee(models.Model):
    deptid = models.ForeignKey(department,on_delete=CASCADE)
    empname = models.CharField(max_length=100)
    age = models.IntegerField()
    address = models.CharField(max_length=500, null=True)
    contactno = models.CharField(max_length=20) 

Create the views functions

views.py

from django.http.response import HttpResponse, JsonResponse
from django.shortcuts import render 
from .models import department,employee 
from django.core import serializers
import json

# Create your views here.

def getdata(request):
    template_name = 'dropdown.html'
    deptcontext = department.objects.all()
    empcontext = employee.objects.all()    

    return render(request,template_name,{'department':deptcontext,                                   'employee':empcontext})

https://labpys.com/how-to-upload-files-images-in-django-python/

Provide Routing (URL patterns)

We need to add dropdown.urls  to main project urls.py

urls.py

 from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include('dropdown.urls')),
]

Create urls.py file inside  dropdown  app to write the URL pattern or providing routing for the application.

urls.py

from django.urls.conf import path
from dropdown import  views 

urlpatterns = [    
    path('getdata/',views.getdata,name="getdata"), 
]

Create Template

            Create a template folder inside  dropdown app and create html file in the directory.

dropdown.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DropDown</title>
   
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>

    <h1><center>How to Create Cascading/Dependent Dropdown List in Django python</center></h1>

  <div class="container">
  <div class="card body">
    <br/>
    <br/>
    <br/>
   
    <div class="row">
    <div class="col-md-4">
        
        <select name="dept" id="department" class="form-control">       
            <option value="0">--Select Department--</option>     
            {% for item in department  %}
            <option value="{{ item.id }}">{{ item.departmentname }}</option>
            {% endfor %}
        </select>
    </div>
  
    <br/>
    <div class="col-md-4">
        <select name="emp" id="Employee" class="form-control">
            <option value="0">--Select Staff--</option>  
            {% for item in employee  %}
            <option value="{{ item.deptid_id }}">{{ item.empname }}</option>
            {% endfor %}
        </select>
    </div>
 
   </div>
  </div>
</div>

 


<script>

$(document).ready(function(){

 var department = $("#department");
 var Employee = $("#Employee");
 var $options = Employee.find('option');
 department.on('change',function(){
    Employee.html($options.filter('[value="'+ this.value  +'"]'));
 }).trigger('change'); 


}); 
</script> 
</body>
</html>

Now make migrations

 Python manage.py makemigrations

Python manage.py migrate

Python manage.py runserver

Please leave your  comments and suggestion

The post How to Create Cascading/Dependent Dropdown List in Django Python appeared first on Tech Insights.

]]>
https://reactconf.org/how-to-create-cascading-dependent-dropdown-list-in-django-python/feed/ 1 397
How to Generate QR Code Using Django https://reactconf.org/how-to-generate-qr-code-using-python/ https://reactconf.org/how-to-generate-qr-code-using-python/#respond Thu, 02 Sep 2021 12:10:00 +0000 http://labpys.com/?p=233 In this article, we will learn how generate a QR code using Django python. Here we generate QR code images from any text. First, we need to install the following …

The post How to Generate QR Code Using Django appeared first on Tech Insights.

]]>
In this article, we will learn how generate a QR code using Django python. Here we generate QR code images from any text.

First, we need to install the following packages

Step-1

  1. pip install qrcode

Step-2

  1. create django project
  2. create django app

then include the app in the settings.py file

Create views.py





from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect 
import io   
import qrcode.image.svg 
import qrcode
  
def qr_code(request): 
    context ={}
    if request.method == "POST":
        factory = qrcode.image.svg.SvgImage
        qr_image = qrcode.make(request.POST.get("http://labpys.com",""),image_factory=factory, box_size=20)    
        bufstore = io.BytesIO()
        qr_image.save(bufstore)    
        context["svg"] = bufstore.getvalue().decode()   
    return render(request,'qrcode.html',context=context)
https://labpys.com/how-to-execute-the-crud-operation-in-django/
Now to set url in urls.py

from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include("qr_code.urls")),
]
 



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

urlpatterns = [
   path("",views.qr_code,name="qrcode"),
]

then Create Template folder in that create HTML file qrcode.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>QR Code Generator</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">  
 
</head>
<body>
  <div class="row col-sm-6 shadow">
    <form method="post">
      {% csrf_token %}
      <h3>generate a QR code(Enter Any text)</h3>
      <div class="row-group">         
        <input type="text"  class="form-control" name="qr_text" autofocus>
      
      <br/>
      <div class="row-group">
        <input type="submit" style="max-width: 200px; margin: auto" class="form-control">
      </div>
      <br/>
    </div>
    </form>
      <div class="row-group">
        <div style="margin: auto">
          {{ svg|safe }}
        </div>
      </div>
  </div>
</body>
</html>

Thank you for reading.

The post How to Generate QR Code Using Django appeared first on Tech Insights.

]]>
https://reactconf.org/how-to-generate-qr-code-using-python/feed/ 0 318
Create a Login Page in Django Python https://reactconf.org/how-to-create-login-page-using-django-python/ https://reactconf.org/how-to-create-login-page-using-django-python/#comments Sun, 29 Nov 2020 19:47:00 +0000 http://labpys.com/?p=1 In this article, we will learn how to Create a Login Page in Django Python. I will explain with the source code https://referworkspace.app.goo.gl/9yR5 Create a Django Login Page To create …

The post Create a Login Page in Django Python appeared first on Tech Insights.

]]>
In this article, we will learn how to Create a Login Page in Django Python. I will explain with the source code

https://referworkspace.app.goo.gl/9yR5

Create a Django Login Page

To create a project just follow the below steps.

Steps:

  1. Create project  

Open the terminal and then run the following command

django-admin startproject adminlogin

  • To Create app in adminlogin project.

Type command in terminal

        cd adminlogin

        python manage.py startapp login

Now it creates the project and app then opens Visual Studio code and opens project adminlogin.

These are steps.

  1. Open settings.py and include the app in the adminlogin project, we need to add a reference to its configuration class in the INSTALLED_APPS setting.
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'login'
]

The next step is to set the root urlsconf at the adminlogin

from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include("login.urls")),
]

Create a urls.py file in the login app.

from django.urls import path
from . import views

urlpatterns = [
    path("",views.base,name="base"),
    path("user_login/",views.user_login,name="user_login"),
    
]

Creating models and defining models in the login app.

from django.db import models

# Create your models here.

class tbl_Authentication(models.Model):
    Empcode = models.IntegerField()
    username = models.CharField(max_length=50,default='')
    password = models.CharField(max_length=50,default='')
    is_active = models.IntegerField(null=True)

    def __str__(self):
        return self.username

    empAuth_objects = models.Manager()

Creating views.py

from django.shortcuts import render,redirect
from django.contrib.auth import login,authenticate
from .models import tbl_Authentication
	# Create your views here.
	
	def base(request):
	    return render(request, 'base.html')
	

	def user_login(request):
	
	    if request.method == 'POST':
	        username = request.POST.get('username')
	        password = request.POST.get('password')
	        
	        try:
	            user = tbl_Authentication.empAuth_objects.get(username=username,password=password)
	            if user is not None:               
	                return render(request, 'dashboard.html', {})
	            else:
	                print("Someone tried to login and failed.")
	                print("They used username: {} and password: {}".format(username,password))
	
	                return redirect('/')
	        except Exception as identifier:
	           
	            return redirect('/')
	
	    else:
	        return render(request, 'base.html')

5. Create a templates folder in the login app.

                In that create base.html file and dashboard.html file, below source copy and paste.

base.html

{% load static %}

<!DOCTYPE html>
<html class=''>
<head>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src='https://production-assets.codepen.io/assets/editor/live/console_runner-079c09a0e3b9ff743e39ee2d5637b9216b3545af0de366d4b9aad9dc87e26bfd.js'></script><script src='//production-assets.codepen.io/assets/editor/live/events_runner-73716630c22bbc8cff4bd0f07b135f00a0bdc5d14629260c3ec49e5606f98fdd.js'></script><script src='//production-assets.codepen.io/assets/editor/live/css_live_reload_init-2c0dc5167d60a5af3ee189d570b1835129687ea2a61bee3513dee3a50c115a77.js'></script><meta charset='UTF-8'><meta name="robots" content="noindex"><link rel="shortcut icon" type="image/x-icon" href="//production-assets.codepen.io/assets/favicon/favicon-8ea04875e70c4b0bb41da869e81236e54394d63638a1ef12fa558a4a835f1164.ico" /><link rel="mask-icon" type="" href="//production-assets.codepen.io/assets/favicon/logo-pin-f2d2b6d2c61838f7e76325261b7195c27224080bc099486ddd6dccb469b8e8e6.svg" color="#111" /><link rel="canonical" href="https://codepen.io/aperyon/pen/oxzpaE?depth=everything&order=popularity&page=23&q=translate&show_forks=false" />

<style class="cp-pen-styles">html, body {
  border: 0;
  padding: 0;
  margin: 0;
  height: 100%;
}

body {
  background: tomato;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 16px;
}

form {
  background: white;
  width: 40%;
  box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.7);
  font-family: lato;
  position: relative;
  color: #333;
  border-radius: 10px;
}
form header {
  background: #FF3838;
  padding: 30px 20px;
  color: white;
  font-size: 1.2em;
  font-weight: 600;
  border-radius: 10px 10px 0 0;
}
form label {
  margin-left: 20px;
  display: inline-block;
  margin-top: 30px;
  margin-bottom: 5px;
  position: relative;
}
form label span {
  color: #FF3838;
  font-size: 2em;
  position: absolute;
  left: 2.3em;
  top: -10px;
}
form input {
  display: block;
  width: 78%;
  margin-left: 20px;
  padding: 5px 20px;
  font-size: 1em;
  border-radius: 3px;
  outline: none;
  border: 1px solid #ccc;
}
form .help {
  margin-left: 20px;
  font-size: 0.8em;
  color: #777;
}
form button {
  position: relative;
  margin-top: 30px;
  margin-bottom: 30px;
  left: 50%;
  transform: translate(-50%, 0);
  font-family: inherit;
  color: white;
  background: #FF3838;
  outline: none;
  border: none;
  padding: 5px 15px;
  font-size: 1.3em;
  font-weight: 400;
  border-radius: 3px;
  box-shadow: 0px 0px 10px rgba(51, 51, 51, 0.4);
  cursor: pointer;
  transition: all 0.15s ease-in-out;
}
form button:hover {
  background: #ff5252;
}
</style>
</head>

<body>

<form method="post" action="user_login/">
 {% csrf_token %}  

  <header>Login</header>
  <label>Username <span>*</span></label>
  <input type="text"   name="username" placeholder="Username" required="" />
  <div class="help">At least 5 character</div>
  <label>Password <span>*</span></label>
  <input type="password"  name="password" placeholder="Password" required="" />
  <div class="help">Use upper and lowercase letters as well</div>
  <button type="submit">Login</button>

</form>

</body>
</html>

dashboard.html

<html>
<head>
</head>
<body>
<p><h3> WelCome dashboard </h3></p>
</body>
</html>


Open the terminal and then run the following command

  • python manage.py makemigrations
  • python manage.py migrate
  • Python manage.py runserver

Recommended Articles:

Django Session | How to Create a Session in Django Python

Find Duplicate Records in Django ORM

How to Create CRUD API in Django Rest Framework

How to Implement Join Operations in Django ORM



The post Create a Login Page in Django Python appeared first on Tech Insights.

]]>
https://reactconf.org/how-to-create-login-page-using-django-python/feed/ 1 1