Категория: Технология программирования

Кол-во просмотров: 7565

Теги: #python #django


Создаем первый проект. Используем модели, делаем обработку в views.py и отображаем объекты в шаблоне

Видео

Исходный код

файл views.py

from django.shortcuts import render

# Create your views here.

from .models import Articles
from django.views.generic import ListView, DetailView



# def home(request):
#     list_articles = Articles.objects.all()
#     context = {
#         'name':'Иван',
#         'list_articles':list_articles
#     }
#     template = 'index.html'
#     return render(request,template,context)
    

    
# def detail_page(request,id):
#     get_article = Articles.objects.get(id=id)
#     context = {
#         'get_article':get_article
#         
#     }
#     
#     template = 'detail.html'
#     
#     return render(request,template,context)


class HomeListView(ListView):
    model = Articles
    template_name = 'index.html'
    context_object_name = 'list_articles'


class HomeDetailView(DetailView):
    model = Articles
    template_name = 'detail.html'
    context_object_name = 'get_article'

файл urls.py (главный)

"""iswork URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('', include('core.urls')),
    path('admin/', admin.site.urls),
]

Файл urls.py в приложение core

"""iswork URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from core import views


urlpatterns = [
#     path('', views.home, name='home'),
#     path('detail/', views.detail_page, name='detail_page'),
    path('', views.HomeListView.as_view(), name='home'),
    path('detail/', views.HomeDetailView.as_view(), name='detail_page'),
    
]

Файл models.py

from django.db import models

# Create your models here.
class Articles(models.Model):
    create_date = models.DateTimeField(auto_now=True)
    name = models.CharField(max_length=200, verbose_name='Название')
    text = models.TextField(verbose_name='Текст')
    
    def __str__(self):
        return self.name
    
    class Meta:
        verbose_name='Статью'
        verbose_name_plural='Статьи'


Категория: Технология программирования

Кол-во просмотров: 7565

Дата создания: 29 ноября 2019 г.

Теги: #python #django