hyeonga_code
파이선 웹구축_장고_31_투표 기능 본문
- 투표 기능 생성
=====================================================
1. 장고 실행
2. acc 앱 생성
3. 기본 설정 <config> > 'settings.py'
'settings.py'
=====
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = 'django-insecure-w5^*mzpxwm@3ps0@pp^v%o0^r(+g9*a=d=6=enn0-l@x2%x4hh'
DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'acc.apps.AccConfig',
]
AUTH_USER_MODEL = 'acc.User'
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR/"media"
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'config.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR/'templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'config.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
STATIC_URL = 'static/'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
cs |
4. 경로 설정 <config> > 'urls.py'
'urls.py'
=====
1
2
3
4
5
6
7
8
9
|
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from . import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('acc/', include('acc.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
cs |
5. 메인 페이지 경로 설정 <acc> > 'urls.py'
- 로그인, 로그아웃 기능까지만 설정
'urls.py'
=====
1
2
3
4
5
6
7
8
9
10
|
from django.urls import path
from . import views
app_name='acc'
urlpatterns = [
path('index/', views.index, name="index"),
path('login/', views.ulogin, name='login'),
path('logout/', views.ulogout, name='logout'),
]
|
cs |
6. 테이블 작성 <acc> > 'models.py'
'models.py'
=====
1
2
3
4
5
|
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
pass
|
cs |
7. 계정 연결 <acc> > 'admin.py'
'admin.py'
=====
1
2
3
4
|
from django.contrib import admin
from .models import User
admin.site.register(User)
|
cs |
>> 계정 3개 정도 생성
8. 메인 페이지 작성 <templates> > <acc> > 'index.html'
'index.html'
=====
1
2
3
4
5
6
7
8
9
10
|
<h1><b>MAIN PAGE</b></h1>
{% if user.is_authenticated %}
#_ 로그인 된 상황
<b>{{user}}</b> wellcome!!!<br><br>
<a href="{% url 'acc:logout' %}"><button>Logout</button></a>
{% else %}
#_ 로그인 전 상황
<a href="{% url 'acc:login' %}"><button>Login</button></a>
|
cs |
9. 로그인, 로그아웃 기능 생성 <acc> > 'views.py'
'views.py'
=====
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.hashers import check_password
def ulogin(request):
if request.user.is_authenticated:
return redirect('index')
if request.method=="POST":
un = request.POST.get('uname')
up = request.POST.get('upass')
u = authenticate(username=un, password=up)
if u:
#_ 로그인 됐을 때
login(request,u)
return redirect('index')
else:
pass
return render(request, 'acc/login.html')
def ulogout(request):
logout(request)
return redirect('index')
def index(request):
return render(request, 'acc/index.html')
|
cs |
10. 로그인 페이지 작성 <templates> > <acc> > 'login.html'
'login.html'
=====
1
2
3
4
5
6
7
8
9
|
<h1><b>LOGIN PAGE</b></h1>
<form method='post'>
{% csrf_token %}
<input type='text' name='uname' placeholder="Input ID"><br><br>
<input type='password' name='upass' placeholder='Input Password'><br><br>
<button>Login</button>
<a href="{% url 'acc:index' %}"><button>Cancle</button></a>
</form>
|
cs |
=====================================================
11. 투표 앱 생성
12. 경로 설정 <config> > 'urls.py'
'urls.py'
=====
1
2
3
4
5
6
7
8
9
10
11
|
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
#_ 밑줄 표시될 경우, 인터프리터 수정(mysite > script)
from . import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('acc/', include('acc.urls')),
path('vote/', include('vote.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
cs |
13. 경로 설정 <vote> > 'urls.py'
'urls.py'
=====
1
2
3
4
5
6
7
8
|
from django.urls import path
from . import views
app_name='vote'
urlpatterns = [
path('index/', views.index, name="index"),
]
|
cs |
14. <templates> > <vote> > 'index.html'
'index.html'
=====
1
2
3
4
5
6
7
8
9
10
11
12
|
<h1><b>MAIN PAGE</b></h1>
{% if user.is_authenticated %}
<b>{{user}}</b> wellcome!!!<br><br>
<a href="{% url 'vote:index' %}"><button>Vote</button></a>
<a href="{% url 'acc:index' %}"><button>Main</button></a>
<a href="{% url 'acc:logout' %}"><button>Logout</button></a>
{% else %}
<a href="{% url 'acc:login' %}"><button>Login</button></a>
{% endif %}
|
cs |
15. 테이블 생성 <vote> > 'models.py'
'models.py'
=====
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
from django.db import models
from acc.models import User
#_ 작성자를 가져옴
class Topic(models.Model):
#_ 토픽과 내용은 1:N관계이므로 테이블 분리
subject = models.CharField(max_length=100)
maker = models.ForeignKey(User, on_delete=models.CASCADE)
#_ ForeignKey(User)
content = models.TextField()
voter = models.ManyToManyField(User, blank=True)
#_ ManyToManyField (set, 지시자)
#_ voter = User 테이블의 레코드들
def __str__(self):
return self.subject
class Choice(models.Model):
#_ 보기 테이블
top = models.ForeignKey(Topic, on_delete=models.CASCADE)
name = models.CharField(max_length=100)
num = models.IntegerField(default=0)
def __str__(self):
return f"{self.top}_{self.name}"
|
cs |
16. 테이블 앱 기본 설정 <config> > 'settings.py'
'settings.py'
=====
1
2
3
4
5
6
7
8
9
10
|
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'acc.apps.AccConfig',
'vote.apps.VoteConfig',
]
|
cs |
>> migrate 안됨
- related_name= >> in models.py
- user입장에서 정보를 알 수 없으므로 연결 고리 알려줘야 함
최종 'models.py'
=====
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
from django.db import models
from acc.models import User
#_ 작성자를 가져옴
class Topic(models.Model):
#_ 토픽과 내용은 1:N관계이므로 테이블 분리
subject = models.CharField(max_length=100)
maker = models.ForeignKey(User, on_delete=models.CASCADE,related_name='maker')
#_ 추가
#_ ForeignKey(User)
content = models.TextField()
voter = models.ManyToManyField(User, blank=True, related_name='voter')
#_ ManyToManyField (set, 지시자)
#_ voter = User 테이블의 레코드들
def __str__(self):
return self.subject
class Choice(models.Model):
#_ 보기 테이블
top = models.ForeignKey(Topic, on_delete=models.CASCADE)
name = models.CharField(max_length=100)
num = models.IntegerField(default=0)
def __str__(self):
return f"{self.top}_{self.name}"
|
cs |
17. 계정 연결 <config> > 'admin.py'
'admin.py'
=====
1
2
3
4
5
|
from django.contrib import admin
from .models import Topic, Choice
admin.site.register(Topic)
admin.site.register(Choice)
|
cs |
>> 마이그레이션
>> topic, choice 테이블 데이터 생성
18. 데이터 넘겨오기 <vote> > 'views.py'
- 투표 페이지에 topic 제목 표시, 제목 클릭 시 상세페이지에 topic의 모든 필드와 choice 출력
'views.py'
=====
1
2
3
4
5
6
7
8
9
|
from django.shortcuts import render
from .models import Topic, Choice
def index(request):
tp = Topic.objects.all()
context = {
'tp' : tp
}
return render(request, 'vote/index.html', context)
|
cs |
19. 페이지에 리스트 작성 <templates> > <vote> > 'index.html'
'index.html'
=====
1
2
3
4
5
6
7
8
9
10
11
|
<h1><b>VOTE PAGE</b></h1>
<b>{{user}}</b> Please vote now!<br><br>
<ul>
{% for i in tp %}
<li><a href="{% url 'vote:detail' i.id %}"><b>{{i.subject}}</b></a><br></li>
{% endfor %}
</ul>
<a href="{% url 'acc:index' %}"><button>Main</button></a>
<a href="{% url 'acc:logout' %}"><button>Logout</button></a>
|
cs |
20. 투표 상세 페이지 경로 설정 <vote> > 'urls.py'
'urls.py'
=====
1
2
3
4
5
6
7
8
9
|
from django.urls import path
from . import views
app_name='vote'
urlpatterns = [
path('index/', views.index, name="index"),
path('detail/<vpk>', views.detail, name='detail'),
]
|
cs |
21. detail 함수 생성 <vote> > 'views.py'
'views.py'
=====
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
from django.shortcuts import render
from .models import Topic, Choice
def detail(request, vpk):
tp = Topic.objects.get(id=vpk)
ch = tp.choice_set.all()
#_ 선택지 정보 넘겨오기
context = {
'tp' : tp,
'cset' : ch,
}
return render(request, 'vote/detail.html', context)
def index(request):
tp = Topic.objects.all()
context = {
'tp' : tp
}
return render(request, 'vote/index.html', context)
|
cs |
22. 상세페이지 작성 <templates> > <vote> > 'detail.html'
'detail.html'
=====
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<h1><b>DETAIL PAGE</b></h1>
<b>{{tp.subject}}</b> <br><br>
Please vote now!<br><br>
<b>{{tp.maker}}</b> <br><br>
<textarea cols='50' rows='4' disabled>{{tp.content}}</textarea><br><br>
<form method='post' action="{% url 'vote:vote' t.id %}">
#_ 인자 필요함
{% csrf_token %}
{% for i in cset %}
<input type='radio' name='cho' value="{{i.id}}" {% if forloop.first %}checked{% endif %}>{{i.name}}
#_ 처음에 체크되어 있음
{% endfor %}
<button>Vote</button>
<a href="{% url 'acc:index' %}"><button type='button'>Main</button></a>
</form>
|
cs |
'Python_Django' 카테고리의 다른 글
파이선 웹구축_장고_32_투표 참여 (0) | 2023.06.24 |
---|---|
파이선 웹구축_장고_기초04_클래스_생성자 (0) | 2023.06.23 |
파이선 웹구축_기초03_함수 (0) | 2023.06.22 |
파이선 웹구축_장고_30_번역 기능 googletrans (0) | 2023.06.22 |
파이선 웹구축_장고_기초02_Import (0) | 2023.06.21 |