userの登録フォームを作りましたが、データベースに保存できません。
そして、フォーム埋めてRgister をクリックした後は{% if registered %} <h2>thank you for registering!</h2>が帰ってきません。またelseのロジックが帰ってきます。
環境
OS WINDOWS 10
PYTHON 3.6
DJANGO 2.2
HTML
html
{% extends 'basic_app/base.html' %} {% load staticfiles %} {% block body_block %} <div class="container"> <div class="jumbotron"> {% if registered %} <h2>thank you for registering!</h2> {% else %} <h2>Register Here</h2> <h3>Fill out the form:</h3> <form enctype="multipart/form-data" method="post"> {% csrf_token %} {{ user_form.as_p }} {{ profile_form.as_p }} <input type="submit" name='' value="Register"> </form> {% endif %} </div> </div> {% endblock %}
MODELS.PY
PYTHON
from django.db import models from django.contrib.auth.models import User class UserProfileInfo(models.Model): user = models.OneToOneField(User,on_delete=models.CASCADE) portfolio_site = models.URLField(blank=True) profile_pic = models.ImageField(upload_to='profile_pics',blank=True) def __str__(self): # method return self.user.username
FORMS.PY
forms
from django import forms from django.contrib.auth.models import User from basic_app.models import UserProfileInfo class UserForm(forms.ModelForm): password = forms.CharField(widget=forms.PasswordInput()) class Meta: model = User # if i want to , I could about that add that in. fields = ('username','email','password') class UserProfileInfoForm(forms.ModelForm): class Meta: model = UserProfileInfo fields = ('portfolio_site','profile_pic')
VIEWS.PY
view
from django.shortcuts import render from basic_app.forms import UserForm, UserProfileInfoForm from django.contrib.auth import authenticate, login,logout from django.http import HttpResponseRedirect, HttpResponse from django.urls import reverse from django.contrib.auth.decorators import login_required # Create your views here. def index(request): return render(request,'basic_app/index.html') @login_required def user_logout(request): logout(request) # reverse back to the indexpage. return HttpResponseRedirect(reverse('index')) @login_required def special(request): return HttpResponse("You are logged in, Nice!") ##################### 登録の関数 def register(request): registered = False if request.method == 'post': user_form = UserForm(data=request.post) profile_form = UserProfileInfoForm(data=request.post) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() # user.set_password(user.set_password) this I said first. jose has different code. user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user if 'profile_pic' in request.FILES: print('found it') profile.profile_pic = request.FILES['profile.pic'] profile.save() registered = True else: print(user_form.errors, profile_form.errors) else: user_form = UserForm() profile_form = UserProfileInfoForm() return render(request, 'basic_app/registration.html', { 'user_form':user_form, 'profile_form':profile_form, 'registered':registered }) ############################################## def user_login(request): if request.method == 'post': username = request.post.get('username') password = request.post.get('password') user = authenticate(username=username, password=password) if user: if user.is_active: login(request,user) return HttpResponseRedirect(reverse('index')) else: return HttpResponse('ACCOUNT IS NOT ACTIVE') else: print("Someone tried to login and failed!") # this is what they tried to log in with. this username and password is not already in our database. print('Username:{} and password {}'.format(username,password)) return HttpResponse('invalid login details supplied!') else: return render(request,'basic_app/login.html',{})
まだ回答がついていません
会員登録して回答してみよう