Posts

Showing posts with the label python

Django Class Based Views Blog App

 Models class Blog(models.Model):     title = models.CharField(max_length =200)     description = models.TextField()     def __str__(self):         return self.title Python manage.py makemigrations Python manage.py migrate views from .models import Blog from django.views.generic.edit import CreateView from django.views.generic.list import ListView from django.views.generic.edit import DeleteView from django.views.generic.edit import UpdateView class BlogCreate(CreateView):     model =  Blog     fields = '__all__'     success_url = '/blog' class BlogList(ListView):     model = Blog class BlogDelete(DeleteView):     model = Blog     success_url = '/list' class BlogUpdate(UpdateView):     model = Blog     fields = '__all__'     template_name = 'dapp/blog_form.html'     success_url = '/list' urls from .views import BlogCreate from .v...

Django Search

 Html Form <form action="{% url 'search' %}" method="GET">       <input type="text" placeholder="Search.." name="q">       <button type="submit">Submit</button>     </form> views.py def query(request):     q = request.GET['q']     results = Image.objects.filter(name__icontains=q)     context= {         'results': results,     }     return render(request,"site.html",context) urls.py urlpatterns = [         path('search/',views.query,name='search'), ]

Django Image Uploads

  Models.py from django.db import models # Create your models here. class Image(models.Model):     name = models.CharField(max_length=50)     image = models.ImageField(blank=True,upload_to='img')     def __str__(self):         return f"{self.name}" Admin.py from django.contrib import admin from .models import Image # Register your models here. admin.site.register(Image) Views.py from django.shortcuts import render from .models import Image # Create your views here. def view(request):     data = Image.objects.all()     context = {         'data':data     }     return render(request,"index.html",context) URLS.py from django.urls import path from . import views urlpatterns = [     path('',views.view), ] HMTL (Index.html) <!DOCTYPE html> <html> <head> <style> ul {   list-style-type: none;   margin: 0px 0px;   padding: 10px 400px;...

Django Model Form

Models.py from django.db import models # Create your models here. class Students(models.Model):     sid = models.CharField(max_length=15)     name = models.CharField(max_length=100)     email = models.EmailField(max_length=50) py manage.py makemigrations students py manage.py  migrate Forms.py from django import forms from django.forms import ModelForm from .models import Students class StudentForm(forms.ModelForm):     class Meta:         model = Students         fields = "__all__" Views.py from django.shortcuts import render from .forms import StudentForm from .models import Students # Create your views here. def students(request):     form = StudentForm()     if request.method == "POST":         form = StudentForm(request.POST)         if form.is_valid():             form.save()     return render(reque...

Flask and Sqlite Basic Crud Python Tutorial

Image
Flask app.py from flask import Flask, render_template, request, redirect import sqlite3 app = Flask(__name__) @app.route('/') def view():     con = sqlite3.connect("product.db")     con.row_factory =sqlite3.Row     cur = con.cursor()     cur.execute("SELECT * FROM PRODUCTS")     rows = cur.fetchall()     return render_template("viewdata.html",rows=rows) @app.route('/add<int:id>') def add(id):     con= sqlite3.connect("product.db")     con.row_factory =sqlite3.Row     cur = con.cursor()     cur.execute("INSERT INTO KART2 (id,name,price) SELECT id,name,price FROM PRODUCTS WHERE id = ?",[id])     con.commit()     cur = con.cursor()     cur.execute("SELECT * FROM kart2")     rows = cur.fetchall()     return render_template("kart.html",rows=rows) @app.route('/kart') def kart():     con = sqlite3.connect("product.db")   ...

Flask Sqlite3 python Web App Tutorial

Image
 1) Creating Database import  sqlite3 conn = sqlite3.connect('bookapp.db') conn.execute("create table Books (id INTEGER PRIMARY KEY AUTOINCREMENT,                name TEXT NOT NULL,                quantity INT  NOT NULL,                edition INT NOT NULL,               year INT NOT NULL,                price INT NOT NULL)") conn.close() 2) need to create template folder and add html files to it 2.1)Html Form  (Template folder) <html> <head>     <title>form</title> </head> <body>  <form action="/input" method="post"> <p>Bookname</p>  <input name="name" type="text" /><br /><br /> <p>Bookquantity</p>  <input name="quantity" type="text" /><br /><br /> ...

Python Tutorial Gui Tkinter Display Data As Text

Image
Displaying data on Tkinter Window from Database  Using the widget  label.config() Example Code: from tkinter import * import sqlite3 root = Tk() root.geometry("200x200") conn = sqlite3.connect('test1.db') def showdata():    cursor = conn.execute("SELECT id, name, address, salary from COMPANY")    for row in cursor:       l1 = Label(root, text="result")       l1.pack()       l1.config(text=row) b1 = Button(root,text="submit",command=showdata) b1.pack() root.mainloop()

Currency Converter Code Python Tkinter Gui

Image
  Window Creation from tkinter import * root = Tk() root.geometry("320x300") root.title("currency converter") i= PhotoImage(file='') root.iconphoto(False,i) root['bg'] ='#f09942' Widgets Using Entry Button & Label lab = Label(root,text="Currency Converter",relief="raised",borderwidth=2,font= ('Garamond',25),bg='#f09942',padx=20,pady=10) lab.pack(padx=10,pady=10) lab_1 =Label(root,text="USD",font= ('Garamond',25),bg='#f09942') lab_1.pack(padx=2,pady=2) en = Entry(root,highlightthickness=1, width=30) en.pack(padx=2,pady=2) bt = Button(root,text="TO",command=convert,width=10) bt.pack(padx=2,pady=2) lab_2=Label(root,text="INR",font= ('Garamond',25),bg='#f09942') lab_2.pack(padx=2,pady=2) en_1=Entry(root,highlightthickness=1, width=30) en_1.pack(padx=2,pady=2) Button Code (Conversion Method)  [Button,command= convert] def convert():     en_1.delet...

Registration Form Python Tkinter GUI Project

Importing Files import tkinter from tkinter import * Initializing Window top=Tk() Declaring Variables name_var = StringVar() email_var = StringVar() pass_var  = StringVar() Function to view Output def submit ():     name = name_var.get()     e= email_var.get()     p = pass_var.get()     print ( "the name is " + name)     print ( "email address is :" + e)     print ( "password is: " + p) Label Function nme = Label(top , text = "name" ).place( x = 30 , y = 50 ) email = Label(top , text = "email" ).place( x = 30 , y = 90 ) password = Label(top , text = "password" ).place( x = 30 , y = 130 ) Button Function sbmitbtn = Button(top , text = "submit" , activebackground = "pink" , activeforeground = "blue" , command =submit).place( x = 30 , y = 170 )  Entry Function   e1 = Entry(top , textvariable =name_var).place( x = 80 , y = 50 ) e2 = Entry(top , textvariable =email_var).pla...