0% found this document useful (0 votes)
17 views

Web Technologies

Web Technologies Assignment Answers

Uploaded by

ar9954010
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Web Technologies

Web Technologies Assignment Answers

Uploaded by

ar9954010
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1. Write a program to swap two numbers without taking a temporary variable.

a = int(input("Enter First Number: "))


b = int(input("Enter Second Number: "))
print("Before Swapping:","a= ",a," and ","b= ",b)
a = a+b
b = a-b
a = a-b
print("After Swapping:","a= ",a," and ","b= ",b)

2. Exponentiation (power of a number)

import math

num = float(input("Enter First Number: "))

n = float(input("Enter the power: "))

value = math.pow(num,n)

print(num," to the power ",n," = ",value)

3. Write a program to find whether the number is even or odd.

num = int(input("Enter First Number: "))

if(num%2==0):

print(num," is even!")

else:

print(num," is odd!")
4. WAP to print a “hello world” in a Django framework.
Ans: The steps to print “hello world” in Django framework are as following:

1. Create a project using “django-admin startproject pythonLab” command.


2. Go inside your project and create a “views.py” file and add the given code to it.

from django.shortcuts import render, HttpResponse

def index(request):

return HttpResponse('Hello World')

3. Now go to “urls.py” and link the “index” function in “views.py” to a URL pattern.

from django.urls import path

from pythonlab import views

urlpatterns = [

path('',views.index,name="index")

4. Now simply execute the command: “python manage.py runserver”.

5. Write a program to connectivity with SQL database.

import mysql.connector as msc

mydb = msc.connect(host="localhost",user="root",password="1234")

myc = mydb.cursor()

myc.execute("show databases")

for x in myc:

print(x)
6. Write a program to manage the session.
1. Configure session settings in your Django project's settings.py file. You can specify options such as the session
engine and the session timeout. Here's a sample session configuration

# settings.py

# Enable sessions

SESSION_ENGINE = "django.contrib.sessions.backends.db"

SESSION_COOKIE_NAME = "my_session_cookie"

SESSION_COOKIE_AGE = 3600 # Session timeout in seconds (1 hour)

2. Create a view to set and retrieve session data. In your app's views.py, you can create a simple view that
sets and retrieves session data:
# views.py

from django.shortcuts import render

from django.http import HttpResponse

def set_session(request):

# Set a session variable

request.session['username'] = 'john_doe'

return HttpResponse("Session data has been set.")

def get_session(request):

# Retrieve and display the session variable

username = request.session.get('username', 'Guest')

return HttpResponse(f"Hello, {username}!")

3. Define URLs for your views in your app's urls.py:

# urls.py

from django.urls import path

from projectName import views

urlpatterns = [ path('set_session/', views.set_session, name='set_session'), path('get_session/',


views.get_session, name='get_session'), ]
7. Write a HTML template file to create a simple form with 5 input fields size. Name, Password, Email,
Pincode, Phone No. and a Submit button.
1. Create a project using “django-admin startproject pythonLab” command.

2. Now in the root directory create a folder named “template” and inside that template folder create a html
file and add the given code.

<html>
<head>
<title></title>
</head>
<style>
form{
font-weight:bold;
width:auto;
border:2px solid black;
display:flex;
flex-direction:column;
justify-content:centre;
}
</style>
<body>
<form>
<p>Name</p><input type="text" />
<p>Password</p><input type="Password" />
<p>Email</p><input type="email" />
<p>Pincode</p><input type="number" />
<p>Phone Number: </p><input type="number" /><br><br>
<input type="submit" value="SUBMIT">
</form>
</body>
</html>

3. Now specify the template path in “setting.py”, inside template section.

“ [os.path.join(BASE_DIR,”template”] “

4. Now create a views.py file and add form method to it.


from django.shortcuts import render, HttpResponse
def form(request):
return render(request,"form.html")

5. In the url.py file map the URL pattern with the views method.
6.
from django.contrib import admin
from django.urls import path
from pythonlab import views
urlpatterns = [
path('',views.form,name="form")
]
6. Now execute the command to run the server.

8. WAP to create a views in a Django application framework.


1. Create a project using “django-admin startproject pythonLab” command.
2. Go inside your project and create a “views.py” file and add the given code to it.
3. Now in the views.py file define views method that will be called by the url.py on matching the url patterns.

from django.shortcuts import render, HttpResponse


def form(request):
return render(request,"form.html")

9. WAP to mapping a URL to views in Django application.


1. Create a project using “django-admin startproject pythonLab” command.
2. Go inside your project and create a “views.py” file and add the given code to it.
3. Now in the views.py file define views method that will be called by the url.py on matching the url patterns.
from django.shortcuts import render, HttpResponse
def form(request):
return render(request,"form.html")

4. Now map this “form” views method to blank url in “urls.py” file.

from django.contrib import admin


from django.urls import path
from pythonlab import views
urlpatterns = [
path('',views.form,name="form")
]
10. Write a python program to display error message if the above validations do not hold.
Email:
import re
# Make a regular expression
# for validating an Emailregex = r'\b[A-Za-z0-9.%+-]+@[A-Za-z0-9.-
]+\.[A-Z|a-z]{2,}\b'
# Define a function for# for validating an Email
def check(email):
# pass the regular expression
# and the string into the fullmatch() method
if(re.fullmatch(regex, email)):
print("Valid Email")
else:
print("Invalid Email")
# Driver Code
if __name_ == '_main_':
# Enter the email
email = "[email protected]"
# calling run function check(email)
email = "[email protected]"
check(email)
email = "ar9954010.com"
check(email)
Name:
name = "aditya"
if name.replace(" ", "").isalpha():
print "Name is valid"
else:
print "Name is invalid"
Password:
import re
password = "@@k@$h@12_12”
flag = 0
while True:
if (len(password)<8):
flag = -1
break
elif not re.search("[a-z]", password):
flag = -1
break
elif not re.search("[A-Z]", password):
flag = -1
break
elif not re.search("[0-9]", password):
flag = -1
break
elif not re.search("[_@$]", password):
flag = -1
break
elif re.search("\s", password):
flag = -1
break
else:
flag = 0
print("Valid Password")
break

if flag ==-1:

print("Not a Valid Password”)

11. Write a program to display "Welcome To Radiant" in the form when the "click" button is clicked.
The form title must be web development using python.

import tkinter as tk
def display_message():
label.config(text="Welcome To Radiant")
# Create the main window
root = tk.Tk()
root.title("Web Development using Python")
# Create a button and associate it with the display_message function
button = tk.Button(root, text="Click", command=display_message)
button.pack()
# Create a label to display the message
label = tk.Label(root, text="")
label.pack()
# Start the main event loop
root.mainloop()

12. To create a "sendSimpleEmail" view to send a simple e-mail.

from django.core.mail import send_mail


from django.http import HttpResponse
def sendSimpleEmail(request,emailto):
res = send_mail("hello dear", "how are you?", "[email protected]", [emailto])
return HttpResponse('%s'%res)

Let’s create a URL to access our view –

from django.conf.urls import patterns, url


urlpatterns = paterns('myapp.views', url(r'^simpleemail/(?P<emailto>
[\w.%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4})/',
'sendSimpleEmail' , name = 'sendSimpleEmail'),)

You might also like