TelenkovDmitry 3 tháng trước cách đây
mục cha
commit
0ed32d581d

+ 5 - 5
courses/django_kind/sitewomen/women/urls.py

@@ -6,11 +6,11 @@ from . import converters
 register_converter(converters.FourDigitYearConverter, "year4")
 
 urlpatterns = [
-    path('', views.index), # http://127.0.0.1:8000
+    path('', views.index, name='home'), # http://127.0.0.1:8000
     # path('details/', views.post_detail),
-    path('posts/<int:year>', views.posts_list),
-    path('cats/<int:cat_id>/', views.categories), # http://127.0.0.1:8000/cats/1/
-    path('cats/<slug:cat_slug>/', views.categories_by_slug), # http://127.0.0.1:8000/cats/asdfad/
+    # path('posts/<int:year>', views.posts_list),
+    path('cats/<int:cat_id>/', views.categories, name='cat_id'), # http://127.0.0.1:8000/cats/1/
+    path('cats/<slug:cat_slug>/', views.categories_by_slug, name='cats'), # http://127.0.0.1:8000/cats/asdfad/
     # re_path(r"^archive/(?P<year>[0-9]{4})", views.archive),
-    path('archive/<year4:year>', views.archive),
+    path('archive/<year4:year>', views.archive, name='archive'),
 ]

+ 13 - 4
courses/django_kind/sitewomen/women/views.py

@@ -1,5 +1,6 @@
-from django.shortcuts import render
-from django.http import HttpResponse, HttpResponseNotFound, Http404
+from django.shortcuts import render, redirect
+from django.http import HttpResponse, HttpResponseNotFound, Http404, HttpResponseRedirect
+from django.urls import reverse
 
 
 # Create your views here.
@@ -19,8 +20,16 @@ def categories_by_slug(request, cat_slug):
 
 
 def archive(request, year):
-    if year > 2025:
-        raise Http404()
+    if year > 2023:
+        # return redirect('/') # код 302
+        # return redirect(index) # код 302 Можно указать функцию-представление
+        # return redirect(index) # код 302 Можно указать функцию-представление
+        # return redirect('home') # указываем имя маршрута
+        # return redirect('/', permanent=True) # код 301
+        uri = reverse('cats', args=('sport', ))
+        print(uri)
+        # return redirect(uri)
+        return HttpResponseRedirect('/') 
     return HttpResponse(f"<h1>Архив по годам</h1><p>{year}</p>")
 
 

+ 83 - 0
courses/jinja/test.py

@@ -0,0 +1,83 @@
+from jinja2 import Template
+from markupsafe import escape
+
+
+class Person:
+    def __init__(self, name, age):
+        self.name = name
+        self.age = age
+
+    def get_name(self):
+        return self.name
+    
+    def get_age(self):
+        return self.age
+
+
+def test_1():
+    name = "Федор"
+    age = 28
+
+    per = Person("Федор", 22)
+
+    tm = Template("Мне {{ p.get_age() }} лет и зовут {{ p.get_name() }}.")
+    msg = tm.render(p = per)
+    print(msg)
+
+'''
+Экранирование и блоки raw, for, if
+'''
+
+data = '''{% raw %}Модуль Jinja вместо
+определения {{ name }}
+подставляет соответствующее значение{% endraw %}'''
+
+link = '''В HTML-документе ссылки определяются как:
+<a href="#">Ссылка</a>'''
+
+def test_2():
+    tm = Template(data)
+    msg = tm.render()
+    print(msg)
+
+def test_3():
+    tm = Template("{{ link | e}}")
+    msg = tm.render(link=link)
+    print(msg)
+
+def test_4():
+    msg = escape(link)
+    print(msg)
+
+
+'''Блок for, if'''
+
+cities = [{'id':1, 'city': 'Москва'},
+          {'id':5, 'city': 'Тверь'},
+          {'id':7, 'city': 'Минск'},
+          {'id':8, 'city': 'Смоленск'},
+          {'id':11, 'city': 'Калуга'}]
+
+link = '''<select name="cities">
+{% for c in cities -%}
+{% if c.id > 6 -%}
+    <option value="{{c['id']}}">{{c['city']}}</option>
+{% elif c.city == "Москва" -%}  
+    <option>{{c['city']}}</option>
+{% else -%}
+    {{c['city']}}
+{% endif -%}
+{% endfor -%}
+</select>'''
+
+def test_5():
+    tm = Template(link)
+    msg = tm.render(cities=cities)
+    print(msg)
+
+
+def main():
+    test_5()
+
+if __name__ == '__main__':
+    main()