Commit dfad726e authored by Isataev Adlet's avatar Isataev Adlet

Создал полностью CRUD для мероприятий

parent cbad5e75
......@@ -3,3 +3,4 @@ Django==3.2.9
pytz==2021.3
sqlparse==0.4.2
Pillow==8.4.0
django-crispy-forms==1.13.0
......@@ -37,7 +37,8 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'webapp'
'webapp',
'crispy_forms'
]
MIDDLEWARE = [
......@@ -125,5 +126,7 @@ STATIC_URL = '/static/'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
CRISPY_TEMPLATE_PACK = 'bootstrap4'
MEDIA_ROOT = os.path.join(BASE_DIR, "uploads")
MEDIA_URL = "/uploads/"
......@@ -17,7 +17,14 @@ from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path
from webapp import views as webapp_views
urlpatterns = [
path('admin/', admin.site.urls),
path("", webapp_views.index_view, name="index"),
path("events/", webapp_views.EventView.as_view(), name="list_events"),
path("events/create/", webapp_views.EventCreateView.as_view(), name="create_events"),
path("events/<int:pk>/", webapp_views.EventDetailView.as_view(), name="detail_events"),
path("events/<int:pk>/edit/", webapp_views.EventUpdateView.as_view(), name="update_events"),
path("events/<int:pk>/delete/", webapp_views.EventDeleteView.as_view(), name="delete_events")
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
from django import forms
from webapp.models import Event
class EventForm(forms.ModelForm):
date = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'YYYY-MM-DD HH-MM-SS'}))
class Meta:
model = Event
fields = ["place", "date"]
......@@ -21,7 +21,7 @@ class Event(models.Model):
date = models.DateTimeField(verbose_name="Дата")
def __str__(self):
return f'{self.place}'
return f'{self.place} - {self.date}'
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css"
integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.5.0/font/bootstrap-icons.css">
<title>{% block title %}{% endblock %}</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="{% url 'index' %}">Speed Dating</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="{% url 'list_events' %}">Мероприятия<span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'create_events' %}">Создать мероприятие</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Редактировать профиль</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Войти</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Регистрация</a>
</li>
</ul>
</div>
</nav>
<div class="container">
{% block content %}{% endblock %}
</div>
</body>
</html>
\ No newline at end of file
{% extends "base.html" %}
{% load crispy_forms_filters %}
{% block title %}Create Event{% endblock %}
{% block content %}
<h1>Создать Мероприятие</h1>
<form action="{% url "create_events" %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form|crispy }}
<button>Создать</button>
</form>
{% endblock %}
\ No newline at end of file
{% extends "base.html" %}
{% block title %}Delete {{ event.place }}{% endblock %}
{% block content %}
<h2>Удалить "{{ event.place }}"</h2>
<a href="{% url "list_events" %}">Cancel</a>
<form action="{% url "delete_events" event.pk %}" method="post">
{% csrf_token %}
<button>Yes</button>
</form>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block title %}Events{% endblock %}
{% block content %}
{{ event.place.name }}
<img src="{{ event.place.photo.url }}" alt="">
{{ event.place.address }}
{{ event.place.description }}
{{ event.date }}
<a href="{% url "update_events" event.pk %}">Редактировать</a>
<a href="{% url "delete_events" event.pk %}">Удалить</a>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block title %}Events{% endblock %}
{% block content %}
{% for event in events %}
<a href="{% url 'detail_events' event.pk %}">{{ event.place }}</a>
{{ event.date }}
<hr>
{% endfor %}
{% endblock %}
\ No newline at end of file
{% extends "base.html" %}
{% load crispy_forms_filters %}
{% block title %}Update Event{% endblock %}
{% block content %}
<h1>Редактировать Мероприятие</h1>
<form action="{% url "update_events" event.pk %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form|crispy }}
<button>Редактировать</button>
</form>
{% endblock %}
\ No newline at end of file
This diff is collapsed.
from django.shortcuts import render
from django.views.generic import ListView
from django.urls import reverse, reverse_lazy
from django.views import View
from django.views.generic import CreateView, DetailView, UpdateView, DeleteView
from webapp.forms import EventForm
from webapp.models import Event
class EventView(ListView):
def index_view(request):
return render(request, "index.html")
class EventView(View):
def get(self, request, *args, **kwargs):
events = Event.objects.all()
context = {"events": events}
return render(request, "event_list.html", context)
class EventCreateView(CreateView):
model = Event
template_name = "event_create.html"
form_class = EventForm
def get_success_url(self):
return reverse("detail_events", kwargs={"pk": self.object.pk})
class EventDetailView(DetailView):
model = Event
template_name = "event_list.html"
ordering = ["date"]
template_name = "event_detail.html"
context_object_name = "event"
class EventUpdateView(UpdateView):
model = Event
template_name = "event_update.html"
form_class = EventForm
def get_success_url(self):
return reverse("detail_events", kwargs={"pk": self.object.pk})
class EventDeleteView(DeleteView):
model = Event
template_name = "event_delete.html"
success_url = reverse_lazy("list_events")
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