无法将数据存储在数据库中

时间:2017-07-21 17:46:58

标签: django django-models django-forms django-views

customers_form.html

      <form>
        <fieldset class='text-center'>
          <legend>Create your Account</legend>
            <div>
                <form action="" method="post" enctype="multipart/form-data">
                    {% csrf_token%}
                    {% include 'registration/form-template.html' %}
                    <div class="form-group">
                        <div align="center" >
                            <input type="submit" class="btn btn-success" value="submit"/>
                            <input type="hidden" name="next" value="{{ next }}" />
                        </div>
                    </div>
                </form>
            </div>
            <br>
            <a href="forgot_password.html">Forgot password?</a>
        </fieldset>
      </form>

形状template.html

{% for field in form %}
    <div class="form-group">
        <div class="col-som-offset-2 col-som-10">
            <span class="text-danger small">{{ field.errors }}</span>
        </div>
        <label >{{ field.label_tag }}{{ field }}</label>
    </div>
{% endfor %}

应用程序/ urls.py

from django.conf.urls import url
from . import views
from django.contrib.auth import views as auth_views

app_name = 'registration'

urlpatterns = [
    url(r'^$', views.IndexView.as_view(), name='registration'),
    url(r'^customer/add/$', views.UserFormView.as_view(), name='customer-add'),

]

凸出/ urls.py

from django.conf import settings
from django.contrib.auth import urls
from django.conf.urls.static import static
from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'', include('registration.urls')),
    url(r'^accounts/', include('django.contrib.auth.urls'))

]

views.py

from django.views import generic
from .models import customers
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
from django.views import View
from .forms import UserForm
from django.contrib.auth.decorators import login_required


class IndexView(generic.ListView):
    template_name = 'registration/home.html'

    def get_queryset(self):
        return customers.objects.all()


class UserFormView(View):
    form_class = UserForm
    template_name = 'registration/customers_form.html'

    # display blank form
    def get(self, request):
        form = self.form_class(None)
        return render(request, self.template_name, {'form': form})

    # process form data
    def post(self,request):
       form = self.form_class(request.POST)

       if form.is_valid():
           user = form.save(commit=False)

           username = form.cleaned_data['username']
           password = form.cleaned_data['password']
           user.set_password(password)
           user.save()

           # return username if credentials are correct
           user = authenticate(username=username, password=password)

           if user is not None:

                if user.is_active:
                   login(request, user)
                   return redirect('registration:index')
       return render(request, self.template_name, {'form': form})

当我点击注册按钮然后它重定向回到注册表单并且不显示任何错误。一切正常,但未在django的用户模型中存储数据。

forms.py

from django import forms
from django.contrib.auth.models import User
from django.forms import ModelForm


class UserForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput)

    class Meta:
        model = User
        fields = [
            'username',
            'email',
            'password',
         ]

我想在django的用户模型中注册用户,但我不能。

1 个答案:

答案 0 :(得分:1)

您的表单标签没有操作......

<form action="" method="post" enctype="multipart/form-data">

action属性告诉表单要发布到的位置。这应该是您的POST数据所在的URL。此外,它看起来没有任何输入...