Showing posts with label Django. Show all posts
Showing posts with label Django. Show all posts

Sunday, July 11, 2021

django - webpage to have personal navigations

 urls.py:

from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.index, name="index"),
path('navigation',views.navigationBar,name="navigationBar")
]

views.py:

from django.http import HttpResponse
from django.shortcuts import render

def navigationBar(request):
return render(request, 'navigationUrls.html')

templates/navigationUrls.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Personal Navigation</title>
</head>
<body>
<h1>Personal Navigations:<br/></h1>
<a href="https://soalicious.blogspot.com/" target="_blank">My Blog Page</a><br/>
<a href="https://www.facebook.com/" target="_blank">Facebook Login Page</a><br/>
<a href="https://twitter.com/?lang=en" target="_blank">Twitter Page</a><br/>
<a href="https://www.instagram.com" target="_blank">Instagram Page</a>
</body>
</html>

WebPage:





Saturday, July 10, 2021

django - homepage - fetch form textarea value from template to views

 template - index.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>template is working</title>
</head>
<body>
<h1>Welcome to text analyzer. please enter your text.</h1>
<form action="/textAnalyze" methlod="get">
<textarea name="text" style="margin: 0px; width: 1307px; height: 111px;"></textarea>
<button type="submit">Analyze Text</button>
</form>
</body>
</html>

urls.py:

from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.index, name="index"),
path('about/',views.about, name="about"),
path('textAnalyze',views.textAnalyze, name="textAnalyze")
]

views.py:

from django.http import HttpResponse
from django.shortcuts import render

def index(request):
params = {"name":"Sri","role":"develoepr"}
# return HttpResponse("Hello")
return render(request,'index.html',params)

def about(request):
return HttpResponse("<h1>about</h1>")
def textAnalyze(request):
ttext = request.GET.get('text','default')
print(ttext)
return HttpResponse(ttext)

web page:

suppose you entered "abcd" in the textarea, then it will go to the views.py and print the inputted data.



django - how to use templates

 Step1: Open settings.py and write templates in the DIR of template section as below:


Step2: Create a templates folder and create a index.html file in it as below:

Contents of index.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>template is working</title>
</head>
<body>
<h1>from index page, Hello</h1>
</body>
</html>

Step3: in the views.py, import the render module from shortcuts and call the template as below:

from django.http import HttpResponse
from django.shortcuts import render

def index(request):
# return HttpResponse("Hello")
return render(request,'index.html')

def about(request):
return HttpResponse("<h1>about</h1>")

The server will run automatically:

You can also create a dictionary and send the same to the template while rendering from views.py and use them in the tempate.

views.py:

from django.http import HttpResponse
from django.shortcuts import render

def index(request):
params = {"name":"Sri","role":"develoepr"}
# return HttpResponse("Hello")
return render(request,'index.html',params)


template - index.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>template is working</title>
</head>
<body>
<h1>from index page, Hello</h1>
{{name}}
</body>
</html>


web page:






Thursday, July 8, 2021

Django - create your first project and runservers and urls and views

Create a project:
Open your pycharm and create  a project with proper name like djangoCodeSource using system interpreter.

Start the project:
Go to pycharm terminal and cd to the project path and run the following cmd.

django-admin
django-admin startproject myFirstSite

now the folder structure will look like as below:



Runserver:
Python manage.py runserver

C:\Users\Srinanda\Desktop\python\djangoCodeSource\myFirstSite>python manage.py runserver
Django version 3.2.5, using settings 'myFirstSite.settings'
Starting development server at http://127.0.0.1:8000/




Create views:
On your project create a python file views.py and create the code:

from django.http import HttpResponse
def index(request):
    Return HttpResponse("hello")




urls.py:

from . import views

urlpatterns=[
path('admins/', admin.site.urls)
path('',views.index, name='index')
path('about/',views.about, name='about')

]






Monday, July 5, 2021

Django - Introduction & Install django and PyCharm CE

 Introduction:

"Django is a high-level Python web framework that enables rapid development of secure and maintainable websites. Built by experienced developers, Django takes care of much of the hassle of web development, so you can focus on writing your app without needing to reinvent the wheel. It is free and open source, has a thriving and active community, great documentation, and many options for free and paid-for support. "

Where did it come from?

"Django was initially developed between 2003 and 2005 by a web team who were responsible for creating and maintaining newspaper websites. After creating a number of sites, the team began to factor out and reuse lots of common code and design patterns. This common code evolved into a generic web development framework, which was open-sourced as the "Django" project in July 2005. "


Django MVT

The MVT (Model View Template) is a software design pattern. It is a collection of three important components Model View and Template. 

  • The Model helps to handle database. It is a data access layer which handles the data.
  • The Template is a presentation layer which handles User Interface part completely.
  •  The View is used to execute the business logic and interact with a model to carry data and renders a template.


Here, a user requests for a resource to the Django, Django works as a controller and check to the available resource in URL. If URL maps, a view is called that interact with model and template, it renders a template. Django responds back to the user and sends a template as a response.


Install Django:

go to the windows power shell and run the following cmd:

pip install django

Check installed django version:

python -m django --version

3.2.5

Check python version:

python --version

Python 3.9.5

Install PyCharm CE:

go to the browser and search with PyCharm download and go to the 1st one Jetbrains.com download PyCharm community edition. It will download around 366MB space.





Featured Post

11g to 12c OSB projects migration points

1. Export 11g OSB code and import in 12c Jdeveloper. Steps to import OSB project in Jdeveloper:   File⇾Import⇾Service Bus Resources⇾ Se...