django-admin.py startproject projectname .
TIME_ZONE = 'Asia/Seoul' STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static') ALLOWED_HOSTS = ['*']
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver 0.0.0.0:80
python manage.py 명령어 --help
python manage.py startapp appname
INSTALLED_APPS = [(기존항목..), 'appname']
from django.db import models from django.utils import timezone class modelname(models.Model): author = models.ForeignKey('auth.User', on_delete=models.CASCADE) title = models.CharField(max_length=200) text = models.TextField() created_date = models.DateTimeField(default=timezone.now) published_date = models.DateTimeField(blank=True, null=True) def publish(self): self.published_date = timezone.now() self.save() def __str__(self): return self.title
python manage.py makemigrations appname
python manage.py migrate appname
admin.site.register(modelname)
path('myapp/', include('appname.urls'))
path('', views.viewname, name='pathname')
from django.shortcuts import render from django.utils import timezone from .models import modelname def viewname(request): objs = modelname.objects.all() return render(request, 'templatename.html', {'keyname': objs})
<html> <body> {% for i in keyname %} <div> <h1><a href="">{{ i.title }}</a></h1> <p>published: {{ i.published_date }}</p> <p>{{ i.text|linebreaksbr }}</p> </div> {% endfor %} </body> </html>
http://127.0.0.1:포트/admin/
에서 모델링된 객체 관리http://127.0.0.1:포트/myapp/
에서 최종 형태 확인{% load static %} <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css"> <link rel="stylesheet" href="{% static 'blog.css' %}">
1. {% block content %} - {% endblock %} 2. {% extends 'base.html' %} : 상속 받고 블럭에 내용 끼워넣기
1. <a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}</a> 2. path('post/<int:pk>/', views.post_detail, name='post_detail')
def post_new(request): if request.method == "POST": form = PostForm(request.POST) if form.is_valid(): post = form.save(commit=False) #post.author = request.user post.published_date = timezone.now() post.save() return redirect('post_detail', pk=post.pk) else: form = PostForm() return render(request, 'post_edit.html', {'form': form})
{% extends 'base.html' %} {% block content %} <h1>New Post</h1> <form method="POST" class="post-form">{% csrf_token %} {{ form.as_p }} <button type="submit" class="save btn btn-default">Save</button> </form> {% endblock %}
def post_edit(request, pk): post = get_object_or_404(Post, pk=pk) if request.method == "POST": form = PostForm(request.POST, instance=post) if form.is_valid(): post = form.save(commit=True) #post.published_date = timezone.now() #post.save() return redirect('post_detail', pk=post.pk) else: form = PostForm(instance=post) return render(request, 'post_edit.html', {'form': form})
{% disqus_show_comments %}
.pd { margin-top: 25px; float: right; } .dt { display: inline-block; margin-top: 15px; margin-bottom: 25px; }