from django.db import models
import datetime
JST = datetime.timezone(datetime.timedelta(hours=9))
# Create your models here.
class Favorite(models.Model):
title = models.CharField('タイトル',max_length=50)
text = models.TextField('本文')
url = models.URLField('URL')
created_at = models.DateTimeField('作成日', default=datetime.datetime.now(tz=JST))
def __str__(self):
return self.title
forms.py
forms.py
from django import forms
from .models import Favorite
class FavoriteForm(forms.ModelForm):
class Meta:
model = Favorite
fields = "__all__"
def __init__(self, *args, **kwargs):
for field in self.base_fields.values():
field.widget.attrs["class"]="form-control"
super().__init__(*args, **kwargs)
views.py
views.py
from django.shortcuts import render
from django.views.generic import CreateView, ListView
from .models import Favorite
from .forms import FavoriteForm
from django.urls import reverse, reverse_lazy
# Create your views here.
class FavoriteCreate(CreateView):
template_name = 'app/favorite_create.html'
form_class = FavoriteForm
success_url = reverse_lazy('app:app/favorite_list')
class FavoriteList(ListView):
template_name = 'app/favorite_list.html'
model = Favorite
pj/urls.py
pj/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('app.urls')),
path('admin/', admin.site.urls),
]
app/urls.py
app/urls.py
from django.urls import path, include
from . import views
app_name = "app"
urlpatterns = [
path('', views.FavoriteCreate.as_view(), name='app/favorite_create'),
path('favorite_list/', views.FavoriteList.as_view(), name='app/favorite_list'),
]
コメント