Advertisement
Coolcap5

Django

Jun 6th, 2024 (edited)
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.63 KB | None | 0 0
  1. order and unorder list   Views.py
  2. from datetime import date
  3. from django.http import HttpResponse
  4. from django.shortcuts import render
  5. from django.template import Context, Template
  6. # Create your views here.
  7. def showlist(request):
  8. fruits=["Mango","Apple","Bananan","Jackfruits"]
  9. student_names=["Tony","Mony","Sony","Bob"]
  10. return
  11. render(request,'showlist.html',{"fruits":fruits,"student_names":student_names}
  12. )
  13. URLS.py
  14. from django.contrib import admin
  15. from django.urls import path, re_path
  16. from ap1.views import check_number, current_date_time
  17. from ap1.views import four_hours_after, four_hours_before
  18. from ap1.views import n_hours_after,display_string
  19. from ap2.views import create_table_of_squares,vc,find_mode
  20. from ap2.views import template_test,showlist
  21. urlpatterns = [
  22. path('admin/', admin.site.urls),
  23. path('cdt/', current_date_time),
  24. path('fha/', four_hours_after),
  25. 7 | P a g e
  26.  
  27. path('fhb/', four_hours_before),
  28. path('nha/<int:num>', n_hours_after),
  29. path('display_string/<slug:sentence>', display_string),
  30. re_path('check_number/(\d){1,2}/',check_number),
  31. path('cts/<int:s>/<int:n>', create_table_of_squares),
  32. path('vc/<str:sentence>', vc),
  33. path('find_mode/<str:listofnum>', find_mode),
  34. path('template_test/', template_test),
  35. path('showlist/', showlist),
  36. ]
  37. Template HTML file (inside ap2/templates subfolder)
  38. showlist.html
  39. <html>
  40. <style type="text/css">
  41. #i1 {background-color: lightgreen;color:brown;display:table}
  42. #i2 {background-color: black;color:yellow}
  43. </style>
  44. <body>
  45. <h1 id="i1">Unordered list of fruits</h1>
  46. <ul>
  47. {% for fruit in fruits %}
  48. <li>{{ fruit }}</li>
  49. {% endfor %}
  50. </ul>
  51. <h1 id="i2">Ordered list of Students</h1>
  52. <ol>
  53. {% for student in student_names %}
  54. <li>{{ student }}</li>
  55. {% endfor %}
  56. </ol>
  57. </body>
  58. </html>
  59.  
  60.  
  61.  
  62.  
  63.  
  64. 2)
  65.  
  66.  
  67. layout   Create basetemp directory under Django project directory  name about
  68. Create layout.html
  69. </head>
  70. <body>
  71.     <header>
  72.         <nav>
  73.             <ul>
  74.                 <li><a href="/home/">Home</a></li>
  75.                 <li><a href="/abot/">About Us</a></li>
  76.                 <li><a href="/contact/">Contact Us</a></li>
  77.                 <!-- Add more navigation links as needed -->
  78.             </ul>
  79.         </nav>
  80.     </header>
  81.  
  82.     <main>
  83.         {% block content %}
  84.         {% endblock %}
  85.     </main>
  86.  
  87.     <footer>
  88.         <p>&copy; {% now "Y" %} Your Website. All rights reserved.</p>
  89.         <p>Developed by Your Name</p>
  90.     </footer>
  91. </body>
  92. </html>
  93.  
  94. Create abt.html
  95. {% extends 'layout.html' %}
  96.  
  97. {% block title %}About Us - Your Website{% endblock %}
  98.  
  99. {% block content %}
  100.     <h1>About Us</h1>
  101.     <!-- Add about us content here -->
  102. {% endblock %}
  103. Create contact.html
  104. {% extends 'layout.html' %}
  105.  
  106. {% block title %}Contact Us - Your Website{% endblock %}
  107.  
  108. {% block content %}
  109.     <h1>Contact Us</h1>
  110.     <!-- Add contact us content here -->
  111. {% endblock %}
  112.  
  113. Create home.html
  114. {% extends 'layout.html' %}
  115.  
  116. {% block title %}Home - Your Website{% endblock %}
  117.  
  118. {% block content %}
  119.     <h1>Welcome to Our Website!</h1>
  120.     <!-- Add home page content here -->
  121. {% endblock %}
  122. Under project about ->create views.py
  123. from django.shortcuts import render
  124.  
  125. def home(request):
  126.     return render(request, 'home.html')
  127.  
  128. def abot(request):
  129.     return render(request, 'abt.html')
  130.  
  131. def contact(request):
  132.     return render(request, 'contact.html')
  133.  
  134. under project about->urls.py
  135. from django.urls import path
  136. from . import views
  137.  
  138. urlpatterns = [
  139.     path('', views.home, name='home'),
  140.     path('abot/', views.abot, name='abot'),
  141.     path('contact/', views.contact, name='contact'),
  142.     # Add more URL patterns as needed
  143. ]
  144. Create path in setting.py->Templates part path set is
  145. DIRS': [BASE_DIR,"basetemp"],
  146.  
  147.  
  148.  
  149.  
  150. 3)
  151.  
  152.  
  153. current date time     In project mysite->urls.py
  154.  
  155. from django.contrib import admin
  156. from django.urls import path, include
  157.  
  158. urlpatterns = [
  159.    path('admin/', admin.site.urls),
  160.    path('', include('first.urls')),
  161. ]
  162. from django.urls import path
  163. from . import views
  164.  
  165. urlpatterns = [
  166. path('current_datetime/', views.current_datetime_view, name='current_datetime')  first app ->views.py
  167.  
  168. from django.http import HttpResponse
  169. from datetime import datetime,timedelta   def current_datetime_view(request):
  170.    # Get the current date and time
  171.    now = datetime.now()
  172.  
  173.    # Return the current date and time as plain text
  174.    return HttpResponse(f"Current Date and Time: {now}")
  175.  
  176.  
  177.  
  178. 4)
  179.  
  180.  
  181. current date time offset     In project mysite->urls.py
  182.  
  183. from django.contrib import admin
  184. from django.urls import path, include
  185.  
  186. urlpatterns = [
  187.    path('admin/', admin.site.urls),
  188.    path('', include('first.urls')),
  189. ]
  190. First app->urls.py
  191. from django.urls import path
  192. from . import views
  193.  
  194. urlpatterns = [
  195. path('datetime_with_offsets/', views.datetime_with_offsets,name='datetime_with_offsets')    first app ->views.py
  196.  
  197. from django.http import HttpResponse
  198. from datetime import datetime,timedelta
  199.  
  200. def datetime_with_offsets(request):
  201. now = datetime.now()
  202. offset_hours = 4
  203. # Calculate dates with offsets
  204. four_hours_ahead = now + timedelta(hours=offset_hours)
  205. four_hours_before = now - timedelta(hours=offset_hours)
  206. html = f"<html><body><h1>Current Date and Time with Offsets:</h1>" \
  207. f"<p>Current: {now}</p>" \
  208. f"<p>Four Hours Ahead: {four_hours_ahead}</p>" \
  209. f"<p>Four Hours Before: {four_hours_before}</p></body></html>"
  210. return HttpResponse(html)
  211.  
  212.  
  213. 5)
  214.  
  215.  
  216. 1: Pip install virtualenv(global installation)
  217. start virtual env
  218.  
  219. 2:python -m virtualenv env
  220. activate virtual env
  221. cd env\scrpts\activate
  222. C:\>cd env
  223.  
  224. C:\env>cd scripts
  225.  
  226. C:\env\Scripts>activate
  227.  
  228. (env) C:\env\Scripts>cd..
  229.  
  230. (env) C:\env>cd..
  231.  
  232. (env) C:\>pip install Django
  233. 4:python>>import Django
  234. check Django version
  235. Django.__version
  236.  
  237.  
  238. (env) C:\>python -m django --version
  239. 5.0.4
  240.  
  241.  
  242. (env) C:\>django-admin startproject mysite
  243.  
  244. (env) C:\>cd mysite
  245.  
  246. (env) C:\mysite>python manage.py runserver
  247. open browse see output
  248. *************************************************
  249.  
  250. C:\>mkdir django
  251.  
  252. C:\>cd django
  253.  
  254. C:\django>pip install virtualenv
  255.  
  256.  
  257. C:\django>python -m virtualenv env
  258. created virtual environment CPython3.12.1.final.0-64 in 802ms
  259.  creator CPython3Windows(dest=C:\django\env, clear=False, no_vcs_ignore=False, global=False)
  260.  seeder FromAppData(download=False, pip=bundle, via=copy, app_data_dir=C:\Users\Lenovo\AppData\Local\pypa\virtualenv)
  261.    added seed packages: pip==24.0
  262.  activators BashActivator,BatchActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
  263.  
  264. C:\django>cd env
  265.  
  266. C:\django\env>cd scripts
  267.  
  268. C:\django\env\Scripts>activate
  269.  
  270. (env) C:\django\env\Scripts>cd..
  271.  
  272.  
  273. (env) C:\django>pip install Django
  274.  
  275. (env) C:\django>python -m django --version
  276. 5.0.4
  277.  
  278. (env) C:\django>django-admin
  279.  
  280. Type 'django-admin help <subcommand>' for help on a specific subcommand.
  281.  
  282. Available subcommands:
  283.  
  284. [django]
  285.    check
  286.    compilemessages
  287.    createcachetable
  288.    dbshell
  289.    diffsettings
  290.    dumpdata
  291.    flush
  292.    inspectdb
  293.    loaddata
  294.    makemessages
  295.    makemigrations
  296.    migrate
  297.    optimizemigration
  298.    runserver
  299.    sendtestemail
  300.    shell
  301.    showmigrations
  302.    sqlflush
  303.    sqlmigrate
  304.    sqlsequencereset
  305.    squashmigrations
  306.    startapp
  307.    startproject
  308.    test
  309.    testserver
  310. Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).
  311.  
  312. (env) C:\django>django-admin startproject mysite
  313.  
  314. (env) C:\django>cd mysite
  315. to open vscode
  316. (env) C:\django\mysite>code .
  317.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement