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

lesson 45

parent ff5e4aa1
......@@ -20,7 +20,7 @@ from webapp import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index_view),
path('articles/add', views.article_create_view),
path('articles/detail', views.article_detail_view),
path('', views.index_view, name='index'),
path('articles/add', views.article_create_view, name='articles_add'),
path('articles/<int:id>', views.article_detail_view, name='articles_detail'),
]
No preview for this file type
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
{% extends 'base.html' %}
{% block content %}
<h1>Create Article</h1>
<form action="/articles/add" method="post">
<form action="{% url 'articles_add' %}" method="post">
{% csrf_token %}
<label>Title:</label><br>
......@@ -25,6 +19,4 @@
<input type="submit" value="Send">
</form>
</body>
</html>
\ No newline at end of file
{% endblock %}
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<a href="/">Вернуться на главную</a>
{% extends 'base.html' %}
{% block content %}
<h1>Article</h1>
<h2>{{ article.title }}</h2>
<p>{{ article.text }}</p>
<p>By: {{ article.author }}</p>
</body>
</html>
\ No newline at end of file
{% endblock %}
\ No newline at end of file
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{% static "css/styles.css" %}">
<link rel="stylesheet" href="{% static "css/bootstrap.min.css" %}">
<title>Document</title>
</head>
<body>
<nav class="navbar navbar-expand-lg bg-body-tertiary">
<div class="container">
<a class="navbar-brand" href="{% url 'index' %}">Navbar</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" 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>
<li class="nav-item">
<a class="nav-link" href="#">Disabled</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container">
{% block content %}
{% endblock %}
</div>
</body>
</html>
\ No newline at end of file
{% load static %}
{% extends 'base.html' %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{% static "css/styles.css" %}">
<link rel="stylesheet" href="{% static "css/bootstrap.min.css" %}">
<title>Document</title>
</head>
<body>
<div class="container">
{% block content %}
<h1>Articles</h1>
<p><a href="/articles/add">Добавить</a></p>
<p><a href="{% url 'articles_add' %}">Добавить</a></p>
{% for article in articles %}
<h2>{{ article.title }}</h2>
<a href="/articles/detail?id={{ article.id }}">Подробнее...</a>
<a href="{% url 'articles_detail' id=article.id %}">Подробнее...</a>
<hr>
{% endfor %}
</div>
</body>
</html>
\ No newline at end of file
{% endblock %}
\ No newline at end of file
# Generated by Django 3.2.23 on 2024-01-08 14:48
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('webapp', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='User',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=200, verbose_name='Имя')),
],
),
migrations.AlterModelOptions(
name='article',
options={'verbose_name': 'статья', 'verbose_name_plural': 'статьи'},
),
migrations.AlterField(
model_name='article',
name='updated_at',
field=models.DateTimeField(auto_now=True, verbose_name='Дата обновления'),
),
]
......@@ -21,3 +21,8 @@ class Article(models.Model):
def __str__(self):
return f'{self.id} {self.title}'
class User(models.Model):
name = models.CharField(max_length=200, verbose_name='Имя')
from django.shortcuts import render
from django.shortcuts import render, get_object_or_404, redirect
from webapp.models import Article
......@@ -8,10 +8,8 @@ def index_view(request):
return render(request, 'index.html', context)
def article_detail_view(request):
# /articles/detail?id=1
article_id = request.GET.get('id')
context = {'article': Article.objects.get(id=article_id)}
def article_detail_view(request, id):
context = {'article': get_object_or_404(Article, id=id)}
return render(request, 'articles/detail.html', context)
......@@ -28,6 +26,4 @@ def article_create_view(request):
author=request.POST.get('author'),
)
context = {'article': article}
return render(request, 'articles/detail.html', context)
return redirect('articles_detail', id=article.id)
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