INSTALLED_APPS = [
...
'projectapp',
]
urlpatterns = [
...
path('projects/', include('projectapp.urls')),
]
from django.urls import path
from projectapp.views import ProjectListView, ProjectCreateView, ProjectDetailView
app_name = 'projectapp'
urlpatterns = [
path('list/', ProjectListView.as_view(), name='list'),
path('create/', ProjectCreateView.as_view(), name='create'),
path('detail/<int:pk>', ProjectDetailView.as_view(), name='detail'),
]
from django.db import models
class Project(models.Model):
image = models.ImageField(upload_to='project/', null=True)
title = models.CharField(max_length=20, null=False)
description = models.CharField(max_length=200, null=True)
created_at = models.DateTimeField(auto_now=True)
from django.forms import ModelForm
from projectapp.models import Project
class ProjectCreationForm(ModelForm):
class Meta:
model = Project
fields = ['image', 'title', 'description']
python manage.py makemigration
python manage.py migrate
from django.shortcuts import render
from django.urls import reverse
from django.views.generic import CreateView, DetailView, ListView
from projectapp.forms import ProjectCreationForm
from projectapp.models import Project
@method_decorator(login_required, 'get')
@method_decorator(login_required, 'post')
class ProjectCreateView(CreateView):
model = Project
form_class = ProjectCreationForm
template_name = 'projectapp/create.html'
def get_success_url(self):
return reverse('projectapp:detail', kwargs={'pk': self.object.pk})
class ProjectDetailView(DetailView):
model = Project
context_object_name = 'target_project'
template_name = 'projectapp/detail.html'
class ProjectListView(ListView):
model = Project
context_object_name = 'project_list'
template_name = 'projectapp/list.html'
paginate_by = 25
{% extends 'base.html' %}
{% load bootstrap4 %}
{% block content %}
<div style="text-align : center; max-width: 500px; margin: 4rem auto;">>
<div class="mb-4">
<h4>Create Project</h4>
</div>
<form action="{% url 'projectapp:create' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% bootstrap_form form %}
<input type="submit" class="btn btn-dark rounded-pill col-6 mt-3">
</form>
</div>
{% endblock %}
{% extends 'base.html' %}
{% block content %}
<div>
<div style="text-align: center; max-width: 500px; margin: 4rem auto;">
<img src="{{ target_project.image.url }}" alt=""
style="height: 12rem; width: 12rem; border-radius: 20rem; margin-bottom: 2rem;">
<h2 style="font-family: 'NanumSquareB'">
{{ target_project.title }}
</h2>
<h5 style="margin-bottom: 3rem;">
{{ target_project.description}}
</h5>
</div>
</div>
{% endblock %}
{% extends 'base.html' %}
{% load static %}
{% block content %}
<style>
.container {
padding: 0
margin: 0, auto;
}
.container a {
width: 45%;
max-width: 250px;
}
.container div {
display: flex;
justify-content: center;
align-items: center;
border-radius: 1rem;
}
.container img {
width: 100%;
border-radius: 1rem;
}
</style>
{% if project_list %}
<div class="container">
{% for project in project_list %}
<a href=" {% url 'projectapp:detail' pk=project.pk %}">
{% include 'snippets/card_project.html' with project=project %}
</a>
{% endfor %}
</div>
<script src="{% static 'js/magicgrid.js' %}"></script>
{% else %}
<div class="text-center">
<h1>No Projects YET! </h1>
</div>
{% endif %}
{% include 'snippets/pagination.html' with page_obj=page_obj %}
<div style="text-align: center;">
<a href="{% url 'projectapp:create' %}" class="btn btn-dark rounded-pill mt-3 mb-3 px-3">
Create Project
</a>
</div>
{% endblock %}
<div>
<img src="{{ project.image.url }}" alt="">
</div>
<div style="text-align: center; margin: 1rem 0;">
{% if page_obj.has_previous %}
<a href="?page={{ page_obj.previous_page_number }}"
class="btn btn-secondary rounded-pill">
{{ page_obj.previous_page_number }}
</a>
{% endif %}
<a href="?page={{ page_obj.number }}"
class="btn btn-secondary rounded-pill active">
{{ page_obj.number }}
</a>
{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}"
class="btn btn-secondary rounded-pill active">
{{ page_obj.next_page_number }}
</a>
{% endif %}
</div>

<div style="display: block; text-align: center;">
<img src="{{ project.image.url }}" alt="">
<h5 class="mt-2">
{{ project.title | truncatechars:8 }}
</h5>
</div>
.pragmatic_logo {
margin-top: 1rem;
font-family: 'Lobster', cursive;
}
.pragmatic_footer_button {
font-size: .10rem;
}
.pragmatic_footer{
text-align:center;
margin-top: 2rem;
}
.pragmatic_header {
text-align:center;
margin: 2rem 0;
}
@media screen and (max-width:500px) {
html {
font-size: 13px;
}
}
a {
color: black;
}
a:hover {
color: black;
text-decoration: none;
}
.pragmatic_header_nav {
margin: 0 0.5rem;
}
.pragmatic_header_navbar {
margin: 1rem 0;
}
![업로드중..]()