Friday, 14 October 2022

Django rest framework Project tutorials part 1

 Create a django project

1.create a environment 

python -m venv myenv

2.activate environment

myenv/Scripts/activate


refer python environment tutorial for more information

https://mamspython.blogspot.com/2022/10/creating-python-envirnment-in-5-ways.html

3.pip install Django

4. pip install djangorestframework

5.  django-admin startproject myproject .

6. python manage.py startapp myapp


7. register installed myapp

go to myproj folder

open settings.py

goto varaible list INSTALLED_APPS = [

add 'myapp', to the list

example

INSTALLED_APPS = [

  'django_crontab',
    'widget_tweaks',

.........

'myapp',

]

8. create urls.py in myapp folder

9. add line in myproj->urls.py 

path('',include('myapp.urls')),

10.add serializers.py file in myapp







Creating python environment in 5 ways

 python environment are created several ways

before creating environment install python software on os level

https://www.python.org/ftp/python/3.10.8/python-3.10.8-amd64.exe

environment are used to separate corresponding projects requirement libraries independent with another projects ,it will also helps to versioning your projects like , one environment may be using python 3.7 version another will be python version 3.8 version


lets create a envirnment myenv

1.python -m venv myenv 

myenv folder is created after executing this comment

inside myenv you can see activate file by opening scripts or bin


there are lots of ways creating environment





to activate the environment,use the below comment

find the path of activate file in my case activate file is located D:\projects\test13\testproj\myenv\Scripts\

so I can activate myenv like 

myenv\Scripts\activate

because i am already in  D:\projects\test13\testproj\ so i need only go from testproj folder "myenv\Scripts\"

or 

by full path

 D:\projects\test13\testproj\myenv\Scripts\activate 



I usually use this method and recommend using this simple way , you can try other methods also

2. python -m virtualenv .

. (the dot is used to create env on the current folder)

for this, you may need to install

pip install virtualenv


3.mkvirtualenv myenv 

using python wrapper

pip install virtualenv
pip install virtualenvwrapper-win ( do not use pip install virtualenvwrapper)

4. you can use pycharm software (download and install pycharm)

5. using anaconda 

Please note:-

Now onwards i will use env for environment 

Wednesday, 11 February 2015

printing triangle pattern


       

word="ayman"
a="*"
c="h"
d="i"
n=0
m=0
while n<10:
    n=n+1
    print(c,end='')
    print(d,end='')
    print(a,end='')
    for b in word:
     print(b,end='')       
    print(a)
    m=0
    print(c,end='')
    while m<=n:
        print(d,end='')
        m=m+1
        
while n>=0:
      n=n-1
      print(a,end='')
      for b in word:
          print(b,end='')
      print(a)
      m=0
      print(c,end='')
      while m<n:
           m=m+1
           print(d,end='')
      print(d,end='')  
        
    

Monday, 2 February 2015

python 3.4.2 part 13 write and read a text file

First create file "readwritefile.py"

/****************************/


       

fw=open("sample.txt","w")
fw.write("i like to swim")
fw.close()
fr=open("sample.txt","r")
textcontent=fr.read()
print(textcontent)
fr.close()

/*********************/
1.fw is variable, it just indicate file write, you can have your own variable
2.fr is variable, it just indicate file read, you can have your own variable
3.open("sample.txt","w"). it create a text file named "sample.txt"
4.and the attribute "w" , w is predefined attribute indicating writing permission
5.and the attribute "r" , w is predefined attribute indicating reading permission
6.close() is used to avoid wastage of memory

python 3.4.2 part 12 downlaoding an image


First install beautifulscraper

pip install beautifulscraper

 

create a new file downimage.py


/******************************/
       

import urllib.request
import random
def download_image(url):
    file_name=random.randrange(1,1000)
    full_file_name=str(file_name)+".jpg"
    urllib.request.urlretrieve(url,full_file_name)
download_image("https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7r5YcS7A_m8OAUsgT7W9M7HUm0vR7BD1Sv2jNnbgaP0tlPpuogQjzQeqlsQK0BRK3YMc4pLWl6m3_JJ_DrvjdUBfLCtkvOv0YFlkLjmB4xTcxLr_cn1uSPsXPPM71e50uf9gQGU3FrvY/s1600/2.jpg")

/***************************************/
run it
1.random is used select a random number
2.first we create download_image(url) function
3.to give a name for downloaded image, we use random function
4.str(file_name) is used to convert  "file_name( number )" to string
5.urllib.request.urlretrieve is a function used to download the image
6."https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7r5YcS7A_m8OAUsgT7W9M7HUm0vR7BD1Sv2jNnbgaP0tlPpuogQjzQeqlsQK0BRK3YMc4pLWl6m3_JJ_DrvjdUBfLCtkvOv0YFlkLjmB4xTcxLr_cn1uSPsXPPM71e50uf9gQGU3FrvY/s1600/2.jpg" this url will pass to def download_image(url):



python 3.4.2 part 11 executing a function from other fileStudent code


Create two files







1.teacher.py
2.student.py




student.py
/******************/
def john():
    print("my name is john")


teacher.py
/*****************/

import student

student.john()






Sunday, 1 February 2015

python 3.4.2 part 10 Add two numbers using def function

File ->new file

copy below code


/********************************/
       

def add_numb(*args):
total=0
for x in args:
total=+x
print(total)
add_numb(3)
add_numb(3,5,6)



/************************************/
then save it
Run->Run module(press f5)