Django-在注册视图中从URL获取数据

时间:2018-12-14 23:30:43

标签: python django django-views

我正在尝试从/ register /?ref = 123456网址中获取数据,但遇到了一些麻烦。

用户注册与我的一种看法有关:

class UserRegisterView(FormView):
    form_class = UserRegisterForm
    template_name = 'accounts/user_register_form.html'
    success_url = '/accounts/login'
    def form_valid(self, form):
        username = form.cleaned_data.get('username')
        email = form.cleaned_data.get('email')
        password = form.cleaned_data.get('password')
        new_user = User.objects.create(username=username, email=email)
        new_user.set_password(password)
        new_user.save()
        ref = self.request.GET.get('ref', 'None')
        print(ref)
        return super(UserRegisterView, self).form_valid(form)

我正在尝试使用“ self.request.GET.get('ref','None')”来抓取数据,但是每次用户注册ref都返回“ None”而不是123456

这是user_register_form.html:

<div class='row'>
    <div class='col-sm-4 offset-sm-4 mt-3'>
        <h1 class='text-center mt-3'>Sign Up</h1>
        <form {% if form_id %} id='{{ form_id }}' {% endif %} class='form mt-3' method='POST' action=''>
            {% csrf_token %}
            {{ form|crispy }}
            <input class='btn btn-primary-new' id="submit" type="submit" value="Sign Up" />
        </form>
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

这是我的解决方案专家: 我替换了

ref = self.request.GET.get('ref', 'None') 

ref = (self.request.GET).dict()
data = ref['ref']

完整实施:

class UserRegisterView(FormView):
    form_class = UserRegisterForm
    template_name = 'accounts/user_register_form.html'
    success_url = '/accounts/login'
    def form_valid(self, form):
        username = form.cleaned_data.get('username')
        email = form.cleaned_data.get('email')
        password = form.cleaned_data.get('password')
        new_user = User.objects.create(username=username, email=email)
        new_user.set_password(password)
        new_user.save()
        ref = (self.request.GET).dict()
        data = ref['ref']
        print(data)
        return super(UserRegisterView, self).form_valid(form)