Commit 90aa34ed authored by Давид Ли's avatar Давид Ли

lesson 45

parent a9f2ce94
...@@ -21,7 +21,9 @@ from web import views ...@@ -21,7 +21,9 @@ from web import views
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('', views.index_view), path('', views.index_view, name='main-page'),
path('articles/add/', views.article_create_view, name='articles-add'), path('articles/add/', views.article_create_view, name='articles-add'),
# /articles/1/ OR /articles/?id=1
# article_details_view(request, id)
path('articles/<int:id>/', views.article_detail_view, name='articles-detail'), path('articles/<int:id>/', views.article_detail_view, name='articles-detail'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
No preview for this file type
<!DOCTYPE html> {% extends 'base.html' %}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Article</h1> {% block title %}
<h1>Article checkout</h1>
{% endblock %}
{% block content %}
<h2>{{ article.title }}</h2> <h2>{{ article.title }}</h2>
<p>{{ article.text }}</p> <p>{{ article.text }}</p>
...@@ -15,5 +13,4 @@ ...@@ -15,5 +13,4 @@
<h4>Created: {{ article.created_at }}</h4> <h4>Created: {{ article.created_at }}</h4>
<h4>Updated: {{ article.updated_at }}</h4> <h4>Updated: {{ article.updated_at }}</h4>
</body> {% endblock %}
</html>
\ No newline at end of file
<!DOCTYPE html> {% extends 'base.html' %}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Create</title>
</head>
<body>
<h1>Create Article</h1> {% block title %}
<form action="/articles/add/" method="post"> <h1>Create Article</h1>
{% csrf_token %} {% endblock %}
<label>Title:</label><br/>
<input type="text" name="title"/><br/>
<label>Text:</label><br/> {% block content %}
<textarea name="text"></textarea><br/> <form action="/articles/add/" method="post">
{% csrf_token %}
<label>Author:</label><br/> <div class="mb-3">
<input type="text" name="author"/><br/> <label for="titleId" class="form-label">Title</label>
<input type="text" name="title" class="form-control" id="titleId" aria-describedby="title">
<br/> </div>
<input type="submit" value="Send"/> <div class="mb-3">
<label for="textId" class="form-label">Text</label>
</form> <textarea name="text" class="form-control" id="textId" cols="30" rows="10"></textarea>
</div>
</body> <div class="mb-3">
</html> <label for="authorId" class="form-label">Author</label>
<input type="text" name="author" class="form-control" id="authorId" aria-describedby="author">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
{% endblock %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.1/css/bootstrap.min.css"
integrity="sha512-Ez0cGzNzHR1tYAv56860NLspgUGuQw16GiOOp/I2LuTmpSK9xDXlgJz3XN4cnpXWDmkNBKXR/VDMTCnAaEooxA=="
crossorigin="anonymous" referrerpolicy="no-referrer"/>
<title>Title</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<a class="navbar-brand" href="{% url 'main-page' %}">Articles</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse flex-grow-0" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="{% url 'articles-add' %}">
Create
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container">
{% block title %}{% endblock %}
{% block content %}{% endblock %}
</div>
{% block script %}{% endblock %}
</body>
</html>
\ No newline at end of file
{% extends 'base.html' %}
{% load static %} {% load static %}
<!doctype html>
<html lang="en"> {% block content %}
<head> <h1>Articles</h1>
<meta charset="UTF-8"> <a href="{% url 'articles-add' %}" class="btn btn-primary">Создать</a>
<meta name="viewport" {% for article in articles %}
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <br>
<meta http-equiv="X-UA-Compatible" content="ie=edge"> <hr>
<link rel="stylesheet" href="{% static 'css/styles.css' %}"> <br>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.1/css/bootstrap.min.css" integrity="sha512-Ez0cGzNzHR1tYAv56860NLspgUGuQw16GiOOp/I2LuTmpSK9xDXlgJz3XN4cnpXWDmkNBKXR/VDMTCnAaEooxA==" crossorigin="anonymous" referrerpolicy="no-referrer" /> <h2>{{ article.title }}</h2>
<title>Hello</title> <p>
</head> <a href="{% url 'articles-detail' id=article.id %}">
<body> Подробнее
<div class="container"> </a>
<h1>Articles</h1> </p>
<a href="{% url 'articles-add' %}" class="btn btn-primary">Создать</a> <br>
{% for article in articles %} <hr>
<br> <br>
<hr> {% endfor %}
<br> {% endblock %}
<h2>{{ article.title }}</h2> \ No newline at end of file
<p>
<a href="{% url 'articles-detail' id=article.id %}">
Подробнее
</a>
</p>
<br>
<hr>
<br>
{% endfor %}
</div>
</body>
</html>
\ No newline at end of file
from django.shortcuts import render, redirect from django.shortcuts import render, redirect, get_object_or_404
from django.urls import reverse
from web.models import Article from web.models import Article
...@@ -6,7 +7,8 @@ from web.models import Article ...@@ -6,7 +7,8 @@ from web.models import Article
def index_view(request): def index_view(request):
return render( return render(
request, 'index.html', context={ request, 'index.html', context={
'articles': Article.objects.order_by('title') 'articles': Article.objects.order_by('title'),
'reverse': reverse('articles-detail', kwargs={'id': 1})
} }
) )
...@@ -30,5 +32,5 @@ def article_create_view(request): ...@@ -30,5 +32,5 @@ def article_create_view(request):
# /articles/{id} # /articles/{id}
def article_detail_view(request, id: int): def article_detail_view(request, id: int):
article = Article.objects.get(id=id) article = get_object_or_404(Article, id=id)
return render(request, 'article.html', context={'article': article}) return render(request, 'article.html', context={'article': article})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment