Reputation: 419
I currently have a form on the “myaccount.html” page of my web application.
This form renders how I want however I cannot submit the form values for some reason to appear in the django admin.
When a user hits “submit”, the page refreshes and the values stay in the input fields.
I’ve tried many methods to solve this but no luck. I thought I had everything together but maybe I’m missing something somewhere?
Is there anything I’m missing to resolve this issue?
I’m new to Django so any help is gladly appreciated.
Thanks!
from users.forms import CustomUserCreationForm
from django.views.generic.edit import CreateView
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.views.decorators.http import require_GET, require_POST
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import redirect
from django.shortcuts import render, redirect
from django.template import loader
from django.urls import reverse_lazy
from django.views import generic
from django.views.generic import FormView
from django.views.generic.edit import FormView
from django.conf import settings
from django import forms
class SignUp(generic.CreateView):
form_class = CustomUserCreationForm
success_url = reverse_lazy('home')
template_name = 'signup.html'
class change(generic.CreateView):
form_class = CustomUserCreationForm
success_url = reverse_lazy('myaccount')
template_name = 'myaccount.html'
def form_valid(self, form):
form.instance.name = self.request.name
form.instance.address = self.request.address
form.instance.zip_code = self.request.zip_code
form.instance.mobile_number = self.request.mobile_number
form.save()
return super(change, self).form_valid(form)
from django.contrib.auth.models import AbstractUser, UserManager
from django.contrib.auth.models import User
from django.db import models
from django.urls import reverse
class CustomUserManager(UserManager):
def get_by_natural_key(self, username):
case_insensitive_username_field = '{}__iexact'.format(self.model.USERNAME_FIELD)
return self.get(**{case_insensitive_username_field: username})
#class CustomUser(AbstractUser):
# objects = CustomUserManager()
class CustomUser(AbstractUser):
# add additional fields in here
name = models.CharField(max_length=100, null=True, blank=True)
address = models.CharField(max_length=100, null=True, blank=True)
zip_code = models.CharField(max_length=100, null=True, blank=True)
mobile_number = models.CharField(max_length=100, null=True, blank=True)
objects = CustomUserManager()
from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import CustomUser
class CustomUserCreationForm(UserCreationForm):
name = forms.CharField(required=True, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Full Name', 'class': 'form-control'}))
address = forms.CharField(required=True, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Address', 'class': 'form-control'}))
zip_code = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Zipcode', 'class': 'form-control'}))
mobile_number = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Mobile Number', 'class': 'form-control'}))
class Meta(UserCreationForm.Meta):
model = CustomUser
fields = ('username', 'email', 'name', 'address', 'zip_code', 'mobile_number')
help_texts = {
'username': '',
'email': '',
# 'password1': 'None',
# 'password2': 'Test'
}
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = CustomUser
fields = ('username', 'email')
from django.conf.urls import url
from django.conf.urls import *
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from . import views
from users import views
from .views import change
urlpatterns = [
# path('change/', views.HomeView.as_view(), name='change'),
# path('myaccount/change/', HomeView.as_view(), name='change'),
# url(r'^myaccount/change/$', views.horse, name='change'),
path('signup/', views.SignUp.as_view(), name='signup'),
path('myaccount/', views.change.as_view(), name='myaccount'),
]
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin
from .forms import CustomUserCreationForm, CustomUserChangeForm
from .models import CustomUser
class CustomUserAdmin(UserAdmin):
add_form = CustomUserCreationForm
form = CustomUserChangeForm
model = CustomUser
list_display = ['email', 'username', 'name', 'address', 'zip_code', 'mobile_number']
admin.site.register(CustomUser, CustomUserAdmin)
HTML
{% block content %}
<form role="form" action="" method="post">
{% csrf_token %}
{{ form.non_field_errors }}
{{ form.name }}
{{ form.name.errors }}
{{ form.address }}
{{ form.address.errors }}
{{ form.zip_code }}
{{ form.zip_code.errors }}
{{ form.mobile_number }}
{{ form.mobile_number.errors }}
<button class="btn btn-primary btn-success btn-round btn-extend" type="submit" value="Submit"><i class="zmdi zmdi-favorite-outline6"></i>Submit</button>
</form>
</div>
{% endblock content %}
Upvotes: 0
Views: 264
Reputation: 599926
You haven't shown the fields or errors for the fields you inherit from the parent class. Without them the form will not be valid. Try adding {{ form.errors }}
to see the full list of validation errors.
Upvotes: 2
Reputation:
For your form_valid method try this instead:
def form_valid(self, form):
user = form.save(commit=False) # object with form data
user.save() # save object
return redirect('success') # and then redirect to success view
Also for your template you can use a for loop to reduce code:
{% for field in form %}
{{ field.label_tag }}
{{ field }}
{% if field.errors %}
{{ field.errors }}
{% endif %}
{% endfor %}
Upvotes: 1