Django- Admin and Users registration Django by Ravinder Nath Rajotiya - January 9, 2022January 10, 20220 Introduction Well, one of the important part of dynamic sites is to create users so that one can have access to the database to enter data, to delete , edit or update the data, to authorize users to have an access to certain data set. Django provide an admin page by default. This can be accessed just by typing admin after the url i.e. localhost:8000/admin and you will see a page as seen below: But you can't do anything here right now. We have to first create super admin who will be responsible for the overall management and users with the roles to have access to certain pages or data. Let us first create a superuser from the command-line. Creating super user Well, If you
blogApp – Forms and database connection Django by Ravinder Nath Rajotiya - January 5, 2022January 5, 20220 blogApp - Forms and database connection In this post we are going to see how to connect forms with database in Django. We explain step by step method. Refer to previous post Simple blogApp in Django This post is extension of Simple blogApp in Django. The following files are created/updated Step-1: Create forms.py and define the fields for table from django import forms from .models import blogPostDb class blogposts(forms.ModelForm): class Meta: model = blogPostDb fields = ['title', 'author', 'contents'] Step-2: Modify the views.py from django.shortcuts import render, redirect from .models import blogPostDb from .forms import blogposts from django.contrib import messages # Create your views here. def home(request): if request.method == 'POST': form = blogposts(request.POST or None) if form.is_valid(): form.save() all_posts=blogPostDb.objects.all() messages.success(request,('Post added.....')) return render(request, 'blog/index.html', {'all_posts': all_posts}) else: all_posts=blogPostDb.objects.all() return render(request, 'blog/index.html', {'all_posts': all_posts}) Step-3: Now modify the home page (index.html) <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title></title> </head> <body> <h1>Welcome home</h1> {% for
Simple blogApp in Django Django by Ravinder Nath Rajotiya - January 4, 2022January 5, 20220 Steps in creating blogProject Starting virtual environment E:\project\webAppProjects>python –m venv venv E:\project\webAppProjects>venv\Scripts\activate Creating Project (venv) E:\project\webAppProjects>django-admin startproject blogProject (venv) E:\project\webAppProjects>cd blogProj (venv) E:\project\webAppProjects\blogProject>django-admin startapp blogApp Run the Server (venv) E:\project\webAppProjects\blogProj>python manage.py runserver Watching for file changes with StatReloader Performing system checks... January 04, 2022 - 09:31:14 Django version 4.0, using settings 'blogProj.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK. now the server is ready, you can test by typing the url http://127.0.0.1:8000/ in the browser Developing Project in Atom Open the Project folder in Atom, your window will look as: Now modify urls.py and settings.py in blogProj folder Now create url.py in blogApp and update url.py and views.py as shown below: Create a folder template in blogApp and create a index.html
Lists in Python PYTHON by Ravinder Nath Rajotiya - April 19, 2019March 12, 20220 Lists List is the most common composite data type in Python. It is is an ordered set of elements. Elements in a list can be of similar or different types. Declaration: A list in python is enclosed inside [ ] bracket. Elements in the list are separated by comma. Example of a list is: list1 = [8,6,7,4,5,3] When a list is created in Python, the interpreter creates a data structure in memory that is similar to an array (but not an array) to store it. The numbering in the list starts with 0, then 1, and so on, if accessed from the end indexing starts from -1, -2 and so on. myList = [3,2,6,4,9,’a’, ‘b’,’c’,‘ram’,’s’] myList 3 2 6 4 ‘a’ ‘b’ ‘c’ ‘ram’ ‘s’ Index from begining 0 1 2 3 4 5 6 7 8 Indexing from end -9 -8 -7 -6 -5 -4 -3 -2 -1 Real-Time Usage of the List The list data
Strings and Lists in Python PYTHON by Ravinder Nath Rajotiya - April 19, 2019March 12, 20220 Strings Strings can be : Single quoted : 'Hello! I am here'. This is a single quoted string Double quoted : "Hello! i am here". This is double "" quoted string. Anything including escape sequence appear as it is, they are overridden Escape sequence be used anywhere (\n, \t, \a, \b and so on) Without print() string appear as it is, but with print () escape sequence have meaning In Python, single-quoted strings and double-quoted strings are the same. Pick a rule and stick to it. Improving readability When a string contains single or double quote characters, however, use the other one to avoid backslashes in the string. It improves readability. Example: We can use raw string with ‘r’ in print() function as: Problems working with strings In many cases,