장고/점프투장고16 점프 투 장고. 실습한 소스 forms.py 모델폼 상속해서 폼 만든 후, 뷰에서 임포트. QuestionForm(request.POST, instance=question) messages.error(request, '에러') forms.py from django import forms from b2b.models import Question, Answer # forms.Form 과 forms.ModelForm class QuestionForm(forms.ModelForm): class Meta: model = Question fields = ['subject', 'content'] labels = { 'subject': '제목', 'content': '내용', } class AnswerForm(forms.ModelForm): cl.. 장고/점프투장고 2023. 11. 18. 회원가입 폼 (부트스트랩) https://7942yongdae.tistory.com/86 장고/점프투장고 2023. 11. 16. 회원 가입view - 글쓴이 추가(모델에 fk 추가) forms.py signup.html views.py def signup(request): 회원 가입 링크 navbar.html {% if not user.is_authenticated %} 회원가입 {% endif %} common/urls.py path('', '회원가입뷰 클래스or함수', name='signup'), from django.contrib.auth import views as auth_views from . import views app_name = 'common' urlpatterns = [ ....... path('signup/', views.signup, name='signup'), ] 회원가입폼UserForm 필요 common/forms.py 필요한 import 모듈은? 필요한 .. 장고/점프투장고 2023. 11. 16. 댓글 수: {% if question.answer_set.count > 0 %} {% if question_list %} {% for question in question_list %} {{ question_list.paginator.count|sub:question_list.start_index|sub:forloop.counter0|add:1 }} {{ question.subject }} {% if question.answer_set.count > 0 %} {{ question.answer_set.count }} {% endif %} {{ question.create_date }} {% endfor %} 장고/점프투장고 2023. 11. 15. 로그인 로그아웃 구현 + 리다이렉트 URL ( config/urls.py ) 03-5 로그인/로그아웃 구현 django.contrib.auth 앱 이용 공통으로 사용되는 기능인 로그인이나 로그아웃을 이 중의 하나의 앱에 종속시키는 것은 좋지 않기 때문이다. 이러한 이유로 여기서는 로그인 · 로그아웃을 "공통 기능을 가진 앱"이라는 의미의 common 앱에 구현할 것이다 common 앱 생성! config/settings.py 에 로그인/아웃 후 이동할 페이지 등록 로그인 url 매핑 로그인 템플릿 만들기 : common/login.html form_errors.html 로그 아웃 및 로그아웃 후 이동할 페이지. LOGIN_REDIRET_URL = '/' urls.py 의 urlpatterns 에 views 의 클래스.as_view() 또는 함수를 등록한다. config/urls... 장고/점프투장고 2023. 11. 15. urls.py <int:pk> <------- views.py : redirect(___, pk=question.id) NoReverseMatch Reverse for 'detail' with arguments '()' and keyword arguments '{} not found. url path 에서 받을 매개변수 pk -------------------- redirect 에서 전달될 곳(pk) 를 잘못 사용한 예. pk 를 뺏으면 문제 없었을 것. 장고/점프투장고 2023. 11. 14. 폼 에러 표시. class , form.errors/field.label/field.errors 폼 에러 표시. form 안에 {% if form.errors %} 를 넣어서, 빈 필드가 있을 경우, submit 이 되지 않게 한다. https://wikidocs.net/70855#_4 2-10 폼 * `[완성 소스]` : [github.com/pahkey/jump2django/tree/2-10](https://github.com/pahkey/jump2django/tree/… wikidocs.net 핑크박스 div class = "alert alert-danger" role = "alert" {% if form.errors %} field.label ---------------- 답변내용 (forms.py 의 label ) field.errors ----------------- 필수 항목입니다. {.. 장고/점프투장고 2023. 11. 13. forms.py 에서 폼디자인( form.as_p 폼위젯, 수동폼 사용) 0. views context 와 return render() 들여쓰기 주의!!!! def question_create(request): form = POST 입력받은 폼 / 빈 폼 저장. context = {'form': form} return render() {{ form.as_p }} 는 html 코드가 자동 생성되므로, 디자인 측면에서 제한이 많다. 수동폼으로 1. 폼 위젯과 레이블 class QuestionForm(forms.ModelForm): class Meta: model = Question fields = ['subject', 'content'] # widgets = { # 'subject': forms.TextInput(attrs={'class': 'form-control'}), # '.. 장고/점프투장고 2023. 11. 12. 테이블 모양 / 부트스트랩 https://getbootstrap.com/docs/5.1/getting-started/introduction/ IntroductionGet started with Bootstrap, the world’s most popular framework for building responsive, mobile-first sites, with jsDelivr and a template starter page.getbootstrap.com {% load static %}{% if question_list %} 출발일 상품번호 상품명 출발확정 .. 장고/점프투장고 2023. 11. 12. settings.py : templates , static, media_url TEMPLATES 'DIRS': [BASE_DIR/'templates'], STATIC_DIR 과 URL STATIC_URL = '/static/' STATICFILES_DIRS = [ BASE_DIR / 'static', ] MEDIA_ROOT 와 MEDIA_URL # 미디어 파일의 경로 설정 MEDIA_ROOT = os.path.join(BASE_DIR, "uploads") MEDIA_URL = "/media/" # 앞에 /를 더하면 절대 경로로 바뀐다. # 127.0.0.1:8000/media/room_photos/1.jpg # 만약 /를 안 붙이면, 내가 이 파일에 접속할 때 상대경로로 URL이 생성된다. -> 지저분해진다. # 127.0.0.1:8000/admin/rooms/photo/1/cha.. 장고/점프투장고 2023. 11. 12. 폼 + 상품의 총 구매수 {{ q.answer_set.count }} 출력 댓글 등록은 상품 예약하기와 같다. 2가지 방법 1) q.answer_set.create(subject=request.POST.get('subject') .............. ) Answer 객체를 사용하지 않기 때문에, save() 메소드를 사용하지 않는다. def answer_create(request, question_id): q = get_object_or_404(Question, pk=question_id) q.answer_set.create(subject = request.POST.get('subject'), content = request.POST.get('content'), create_date=timezone.now()) return redirect('b2b:detail', q.id.. 장고/점프투장고 2023. 11. 12. NoReverseMatch at /b2b/2/ Reverse for '....' with arguments 오류 NoReverseMatch at /b2b/2/ Reverse for '....' with arguments 오류 템플릿에 url 태그 사용하면, 전달할 인자가 잘못 됐다는 말. Reverse for 'answer_create' with arguments '('',)' not found. 장고/점프투장고 2023. 11. 12. url 네임스페이스(app_name) 과 url 별칭. {% url '' , redirect 템플릿 페이지 링크 하드코딩-->url 태그로 바꾸기 하드코딩 : {{ question.subject }} {{ question.subject }} 파라미터명 전달 한 개의 파라미터를 전달할 경우에는 다음과 같이 사용했다. {% url 'detail' question.id %} 이 때 다음처럼 파라미터 명을 함께 사용할수 있다. {% url 'detail' question_id=question.id %} 만약 2개 이상의 파라미터를 사용해야 한다면 다음과 같이 공백 문자 이후에 덧 붙여주면 된다. {% url 'detail' question_id=question.id page=2 %} 여러개 의 앱이 있을 경우 있으니, url 네임스페이스 사용 redirect 함수와 URL 별칭 URL별칭은 템플릿 외에.. 장고/점프투장고 2023. 11. 12. 장고 템플릿 문법 https://docs.djangoproject.com/en/4.0/topics/templates/ Django The web framework for perfectionists with deadlines. docs.djangoproject.com https://wikidocs.net/70736 2-04 조회와 템플릿 * `[완성 소스]` : [github.com/pahkey/jump2django/tree/2-04](https://github.com/pahkey/jump2django/tree/… wikidocs.net 장고/점프투장고 2023. 11. 12. 장고 objects 쿼리 조회/ 추가 / 수정 / 답글 / 답글 조회 https://docs.djangoproject.com/en/4.0/topics/db/queries/ Django The web framework for perfectionists with deadlines. docs.djangoproject.com Question 수정 https://wikidocs.net/70650 2-02 모델 * `[완성 소스]` : [github.com/pahkey/jump2django/tree/2-02](https://github.com/pahkey/jump2django/tree/… wikidocs.net 이번에는 저장한 Question 데이터를 수정해 보자. 먼저 다음과 같이 id 값이 2인 데이터를 조회한다. >>> q = Question.objects.get(id=2) >.. 장고/점프투장고 2023. 11. 11. 점프 투 장고. 재실습 python manage.py sqlmigrate b2b 0001 python manage.py makemigrations migrate 하기 전에 쿼리문을 살펴볼 수 있다. (venv38) {0:30}~/github/tws ➭ python manage.py sqlmigrate b2b 0001 BEGIN; -- -- Create model Question -- CREATE TABLE "b2b_question" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "subject" varchar(200) NOT NULL, "content" text NOT NULL, "create_date" datetime NOT NULL); -- -- Create model Answer.. 장고/점프투장고 2023. 11. 11. 이전 1 다음