🎑

15. 카페 등록 페이지

admin 페이지를 이용해서 카페를 등록할 수 있지만, 이는 관리자만 접속할 수 있으므로 일반 사용자는 사용이 불가능 합니다. 그래서 이번에는 유저가 카페를 등록할 수 있는 페이지를 만들어보겠습니다.
 
/write URL에 write.html 페이지가 렌더링 되도록 하겠습니다.
from django.contrib import admin from django.urls import path from main.views import index, about, write, cafelist, cafedetails from django.conf.urls.static import static from django.conf import settings urlpatterns = [ path('admin/', admin.site.urls), path('', index, name='index'), path('about/', about, name='about'), path('write/', write, name='write'), path('cafelist/', cafelist, name='cafelist'), path('cafelist/<int:pk>/', cafedetails, name='cafedetails'), ] urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urls.py
 
from django.shortcuts import render, redirect from .models import Cafe (...중략...) def write(request): if request.method == 'POST': data = { 'name': request.POST.get('name'), 'location': request.POST.get('location'), 'phone': request.POST.get('phone'), 'content': request.POST.get('content'), 'mainphoto': request.FILES.get('mainphoto'), 'subphoto': request.FILES.get('subphoto'), } cafe = Cafe.objects.create(**data) return redirect(f'/cafelist/{cafe.pk}/') context = { 'locations': Cafe.locations, } return render(request, 'main/write.html', context)
views.py
 
notion imagenotion image