如何在Django-registration 1.0中添加额外的(自定义)字段

时间:2014-10-29 14:02:14

标签: django django-registration

我一直在阅读很多有关此问题的答案,但仍然没有好运。例如here是一个很好的答案,但很可能不适用于django-registration 1.0。

我的目标是在注册表单中添加两个自定义字段,即组织位置注意:我正在使用 registration.backend.simple 提供的一步式django注册。

1 个答案:

答案 0 :(得分:0)

由于你还没有得到答案,我提供了这个答案,虽然它并不完全是你所要求的。不管怎样,我认为它可能对你有所帮助......

这是我在使用django-registration 1.0,Python 2.7和Django 1.6的几个项目中完成的。在这种情况下,我只需使用emailpasswordaccounts进行注册,然后用户可以在注册后添加个人资料字段。在注册时接受这些字段不应该太难以修改它。

我使用Twitter Bootstrap进行样式设置,因此我的模板可能会帮助您,也可能不会帮助您。在这种情况下,我把它们排除了。

我创建了一些名为authenticationfrom django.db import models class UserProfile(models.Model): user = models.OneToOneField(User) title = models.CharField(max_length=100) company = models.CharField(max_length=250) 的应用,用于保存我的模型,表单和视图:

<强> accounts.models.UserProfile

from django.forms import ModelForm

class EditUserProfileForm(ModelForm):**
    . . .
    title = forms.CharField(widget=forms.TextInput())
    company = forms.CharField(widget=forms.TextInput())
    . . .

<强> authentiction.forms.EditUserProfileForm

from django.views.generic.base import View
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponseRedirect

from .models import UserProfile
from .forms import EditUserProfileForm

class EditUserProfileView(View):

    form_class = EditUserProfileForm
    template_name = 'accounts/profile_edit.html'

    @method_decorator(login_required)
    def get(self, request, *args, **kwargs):

        profile = get_object_or_404(UserProfile, user=request.user)

        form_dict = {
            'title': profile.title,
            'company': profile.company,
        }

        form = self.form_class(form_dict)

        return render(request, self.template_name, {
            'form': form,
            'profile': profile,
        })

    @method_decorator(login_required)
    def post(self, request, *args, **kwargs):

        profile = get_object_or_404(UserProfile, user=request.user)

        form = self.form_class(request.POST, instance=profile)

        if form.is_valid():

            company = form.cleaned_data['company']
            title = form.cleaned_data['title']

            title.company = title
            profile.company = company

            # you might need to save user too, depending on what fields
            request.user.save()
            profile.save()

            return HttpResponseRedirect('/dashboard/')

        return render(request, self.template_name, {'form': form})

<强> accounts.views.EditUserProfileView

url(r'^accounts/profile/edit/', EditUserProfileView.as_view(),  name='edit_user_profile_view'),

<强> project.urls

require 'java'

java_import java.net.URI
java_import java.sql.DriverManager

def do_database
  connection = get_connection

  stmt = connection.createStatement
  stmt.executeUpdate("CREATE TABLE IF NOT EXISTS ticks (tick timestamp)")
  stmt.executeUpdate("INSERT INTO ticks VALUES (now())")
  rs = stmt.executeQuery("SELECT tick FROM ticks")

  rs.next 
  begin
    puts "Read from DB: " + rs.getTimestamp("tick")
  end while rs.next

ensure
  connection.close if connection != nil
end

def get_connection
  dbUri = ENV["DATABASE_URL"]

  username = dbUri.getUserInfo.split(":")[0]
  password = dbUri.getUserInfo.split(":")[1]
  port = dbUri.getPort

  dbUrl = "jdbc:postgresql://#{dbUri.getHost}:#{port}#{dbUri.getPath}"

  DriverManager.getConnection(dbUrl, username, password)
end