Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
from django.contrib import admin
from django.urls import path

from helloapp.views import hello_world_view
from helloapp.views import application_form_view, event_detail_view, event_list_view

urlpatterns = [
path('admin/', admin.site.urls),
path('', hello_world_view, name='hello_world'),
path('', event_list_view, name='event_list'),
path('events/<int:event_id>/', event_detail_view, name='event_detail'),
path('events/<int:event_id>/apply/', application_form_view, name='application_form'),
]
9 changes: 9 additions & 0 deletions src/helloapp/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django import forms

from .models import Application


class ApplicationForm(forms.ModelForm):
class Meta:
model = Application
fields = ["applicant_name", "applicant_email"]
16 changes: 16 additions & 0 deletions src/helloapp/templates/helloapp/application_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>申込みフォーム</title>
</head>
<body>
<h1>イベント申込みフォーム</h1>
<p>イベント: {{ event.title }}</p>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">送信</button>
</form>
</body>
</html>
17 changes: 17 additions & 0 deletions src/helloapp/templates/helloapp/event_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>イベント詳細</title>
</head>
<body>
<h1>{{ event.title }}</h1>
<p>開始日時: {{ event.start_date|date:"Y-m-d H:i" }}</p>
<p>終了日時: {{ event.end_date|date:"Y-m-d H:i" }}</p>
<p>定員: {{ event.capacity }}</p>
<p>{{ event.description }}</p>

<!-- 申込みフォームページへのリンク -->
<p><a href="{% url 'application_form' event.id %}">申込みフォームへ進む</a></p>
</body>
</html>
19 changes: 19 additions & 0 deletions src/helloapp/templates/helloapp/event_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>イベント一覧</title>
</head>
<body>
<h1>イベント一覧</h1>
<ul>
{% for event in events %}
<li>
<a href="{% url 'event_detail' event.id %}">
{{ event.title }}
</a> ({{ event.start_date|date:"Y-m-d H:i" }} ~ {{ event.end_date|date:"Y-m-d H:i" }})
</li>
{% endfor %}
</ul>
</body>
</html>
4 changes: 2 additions & 2 deletions src/helloapp/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@


@pytest.mark.django_db
def test_hello_world():
def test_event_list():
client = Client()
response = client.get("/")
assert response.status_code == 200
assert b"Hello World" in response.content
assert "イベント一覧" in response.content.decode('utf-8')


@pytest.mark.django_db
Expand Down
33 changes: 33 additions & 0 deletions src/helloapp/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, redirect, render

from .forms import ApplicationForm
from .models import Event


def hello_world_view(request):
return HttpResponse("Hello World")


def event_list_view(request):
events = Event.objects.all().order_by('start_date')
return render(request, "helloapp/event_list.html", {"events": events})


def event_detail_view(request, event_id):
event = get_object_or_404(Event, id=event_id)
return render(request, "helloapp/event_detail.html", {"event": event})


def application_form_view(request, event_id):
event = get_object_or_404(Event, id=event_id)

if request.method == "POST":
form = ApplicationForm(request.POST)
if form.is_valid():
application = form.save(commit=False)
application.event = event
application.save()
return redirect('event_detail', event_id=event.id)
else:
form = ApplicationForm()

return render(request, 'helloapp/application_form.html', {
'form': form,
'event': event,
})
Loading