Exemple #1
0
def register(request):
    if request.method == 'POST':
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)

        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.save()
            prof = profile_form.save(commit=False)
            prof.user = user
            if 'picture' in request.FILES:
                prof.picture = request.FILES['picture']
                print prof.picture
                print type(prof.picture)
            prof.save()
            username = request.POST.get('username')
            password = request.POST.get('password')
            user = authenticate(username=username, password=password)
            login(request, user)
            request.session['notice'] = u'注册成功*%s*' % username
            return redirect('/blog/')
        else:
            print user_form.errors
            error = user_form.errors.as_data().values()[0][0].messages[0]
            print type(error)
            return render(request, 'blog/auth/register.html',
                          {'error': error})
    else:
        return render(request, 'blog/auth/register.html')
Exemple #2
0
def register(request):
	context = RequestContext(request)

	registered = False

	if request.method=='POST':
		user_form = UserForm(data=request.POST)
		profile_form = UserProfileForm(data=request.POST)

		if user_form.is_active() and profile_form.is_active:
			user = user_form.save()
			user.set_password(user.password)
			user.save()

			profile = profile_form.save(commit=False)
			profile.user = user

			if 'picture' in request.FILES:
				profile.picture = request.FILES['picture']

			profile.save()
			registered=True

		else:
			print(user_form.errors, profile_form.errors)

	else:
		user_form = UserForm()
		profile_form = UserProfileForm()

	return render_to_response(
		'blog/register.html',
		{'user_form':user_form, 'profile_form':profile_form, 'registered':registered},
	context)					
Exemple #3
0
def register(request):
    # Like before, get the request's context.
    context = RequestContext(request)

    # A boolean value for telling the template whether the registration was successful.
    # Set to False initially. Code changes value to True when registration succeeds.
    registered = False

    # If it's a HTTP POST, we're interested in processing form data.
    if request.method == 'POST':
        # Attempt to grab information from the raw form information.
        # Note that we make use of both UserForm and UserProfileForm.
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)

        # If the two forms are valid...
        if user_form.is_valid() and profile_form.is_valid():
            # Save the user's form data to the database.
            user = user_form.save()

            # Now we hash the password with the set_password method.
            # Once hashed, we can update the user object.
            user.set_password(user.password)
            user.save()

            # Now sort out the UserProfile instance.
            # Since we need to set the user attribute ourselves, we set commit=False.
            # This delays saving the model until we're ready to avoid integrity problems.
            profile = profile_form.save(commit=False)
            profile.user = user

            # Did the user provide a profile picture?
            # If so, we need to get it from the input form and put it in the UserProfile model.
            if 'picture' in request.FILES:
                profile.picture = request.FILES['picture']

            # Now we save the UserProfile model instance.
            profile.save()

            # Update our variable to tell the template registration was successful.
            registered = True

        # Invalid form or forms - mistakes or something else?
        # Print problems to the terminal.
        # They'll also be shown to the user.
        else:
            print user_form.errors, profile_form.errors

    # Not a HTTP POST, so we render our form using two ModelForm instances.
    # These forms will be blank, ready for user input.
    else:
        user_form = UserForm()
        profile_form = UserProfileForm()

    # Render the template depending on the context.
    return render_to_response(
            'register.html',
            {'user_form': user_form, 'profile_form': profile_form, 'registered': registered},
            context)
Exemple #4
0
def register(request):
    # boolean value
    # Установлено в False при инициализации.
    # Изменим на True при успешной регистрации.

    registered = False

    # Если HTTP POST, обработаем форму.
    if request.method == 'POST':
        # Получаем информацию из форм.
        # Мы используем две формы UserForm и UserProfileForm.
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)
        # Если обе формы прошли проверку...
        if user_form.is_valid() and profile_form.is_valid():
            # Сохраним данные пользователя из формы в database.
            user = user_form.save()

            # Хешируем пароль с помощью set_password method.

            user.set_password(user.password)
            user.save()
            # Пока пользователь настраивает свой профиль не выполнять commit=False.

            profile = profile_form.save(commit=False)
            profile.user = user
            # Юзер хочет картинку?
            # Если да, предоставим ему поле для ввода картинки.
            if 'picture' in request.FILES:
                profile.picture = request.FILES['picture']

            # Сохранить экземпляр модели UserProfile.
            profile.save()

            # Изменить переменную при успешной регистрации.
            registered = True
        else:
            print user_form.errors, profile_form.errors
            # Не HTTP POST, строим два экземпляра ModelForm .
    # Эти формы пустые , предназначены для пользовательских вводов.
    else:
        user_form = UserForm()
        profile_form = UserProfileForm()


    # Render the template depending on the context.
    return render(request, 'blog/register.html',
                  {'user_form': user_form, 'profile_form': profile_form, 'registered': registered})
Exemple #5
0
def update_profile(request):
    if request.method == 'POST':
        user_form = UserUpdateForm(data=request.POST, instance=request.user)
        user_profile_form = UserProfileForm(request.POST,
                                            request.FILES,
                                            instance=request.user.profile)

        if user_form.is_valid() and user_profile_form.is_valid():
            user = user_form.save()
            user.refresh_from_db()
            user_profile_form.save(user=user)
            return redirect('index')
    else:
        try:
            profile = Profile.objects.get(pk=request.user.profile.id)
        except Profile.DoesNotExist:
            raise Http404('Invalid Profile')
        else:
            user_form = UserUpdateForm(instance=profile.user)
            user_profile_form = UserProfileForm(instance=profile)

    return render(request,
                  'blog/edit_profile.html',
                  context={
                      'user_form': user_form,
                      'user_profile_form': user_profile_form
                  })
Exemple #6
0
def register(request):
    tagss = Tag.objects.all()
    posts = Post.objects.order_by('-created_date')
   
    for tag in tagss:
       q = Post.objects.filter(tags=tag)
       z = q.count()>2
       if z:
          x=True
          tag.important=x
          tag.save()
    tags=Tag.objects.filter(important=True)
    # Like before, get the request's context.
    
    context = RequestContext(request)

    # A boolean value for telling the template whether the registration was successful.
    # Set to False initially. Code changes value to True when registration succeeds.
    registered = False

    # If it's a HTTP POST, we're interested in processing form data.
    if request.method == 'POST':

        user_form = UserForm(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)

        if user_form.is_valid() and profile_form.is_valid():
               
            user = user_form.save()
            user.set_password(user.password)
            user.save()
            profile = profile_form.save(commit=False)
            profile.user = user
            profile.save()
            registered = True                  
        else:
            print user_form.errors, profile_form.errors

    else:
        user_form = UserForm()
        profile_form = UserProfileForm()
    return render_to_response(
            'blog/register.html',
            {'user_form': user_form, 'profile_form': profile_form, 'registered': registered,'tags':tags},
            context)
Exemple #7
0
def signup(request):
    if request.method == 'POST':
        user_form = UserForm(request.POST)
        user_profile_form = UserProfileForm(request.POST, request.FILES)

        if user_form.is_valid() and user_profile_form.is_valid():
            user = user_form.save()
            user.refresh_from_db()
            user_profile_form.save(user=user)
            return redirect('index')
    else:
        user_form = UserForm()
        user_profile_form = UserProfileForm

    return render(request,
                  'blog/signup.html',
                  context={
                      'user_form': user_form,
                      'user_profile_form': user_profile_form
                  })
Exemple #8
0
    def post(self, request):
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)

        # If the two forms are valid...
        if user_form.is_valid() and profile_form.is_valid():
            # Save the user's form data to the database.
            user = user_form.save()
            #Now sort out the UserProfile instance.
            # Since we need to set the user attribute ourselves, we set commit=False.
            profile = profile_form.save(commit=False)
            profile.user = user
            # Now we save the UserProfile model instance.
            profile.save()
            return redirect('/')
        else:
            context = {
                'user_form': user_form,
                'profile_form': profile_form
            }
            return render(request, self.template, context)
Exemple #9
0
def register(request):
    if request.method == "POST":
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)
        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()
            # Hash password
            user.set_password(user.password)
            user.save()

            profile = profile_form.save(commit=False)
            profile.user = user

            if 'profile_pic' in request.FILES:
                profile.profile_pic = request.FILES['profile_pic']

            profile.save()
            login(request, user)
            return HttpResponseRedirect(reverse('blog:post_list'))
        else:
            print(user_form.errors, profile_form.errors)
            return render(request, "registration/register.html", {
                'user_form': user_form,
                'profile_form': profile_form
            })
    else:
        if request.user.is_authenticated:
            return redirect("/")

        user_form = UserForm()
        profile_form = UserProfileForm()
        return render(request, "registration/register.html", {
            'user_form': user_form,
            'profile_form': profile_form
        })
Exemple #10
0
def register(request):
    if request.method == "GET":
        user_form = UserForm()
        user_profile_form = UserProfileForm()
        context = {
            'user_form': user_form,
            'user_profile_form': user_profile_form,
        }
        return render(request, "blog/register.html", context)
    if request.method == "POST":

        user_form = UserForm(request.POST)
        user_profile_form = UserProfileForm(request.POST)

        if user_form.is_valid() and user_profile_form.is_valid():

            user_object = user_form.save()

            user_profile_object = user_profile_form.save(commit=False)

            user_profile_object.user = user_object

            user_profile_object.save()

        else:
            pass

        return redirect('blog:index')
Exemple #11
0
def register(request):
    register = False
    if request.method == 'POST':
        user_form = Userform(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)
        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.save()
            profile = profile_form.save(commit=False)
            profile.user = user
            if 'picture' in request.FILES:
                profile.picture = request.FILES['picture']
            profile.save()
            register = True
        else:
            print user_form.errors, profile_form.errors
    else:
        user_form = Userform()
        profile_form = UserProfileForm()
    return render(
        request, 'blog/register.html', {
            'user_form': user_form,
            'profile_form': profile_form,
            'registered': register
        })
Exemple #12
0
def register(request):
    if request.method=='POST':
        user_form=UserForm(data=request.POST)
        profile_form=UserProfileForm(data=request.POST)

        if user_form.is_valid() and profile_form.is_valid():
            user=user_form.save()
            user.set_password(user.password)
            user.save()
            profile=profile_form.save(commit=False)
            profile.user=user
            profile.save()

            return HttpResponseRedirect("/blog/")

    else:
        user_form = UserForm()
        profile_form = UserProfileForm()

    return render(request,
        'blog/register.html',
        {'user_form': user_form, 'profile_form': profile_form } )
Exemple #13
0
def register(request):
    register = False
    if request.method == "POST":
        user_form = Userform(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)
        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.save()
            profile = profile_form.save(commit=False)
            profile.user = user
            if "picture" in request.FILES:
                profile.picture = request.FILES["picture"]
            profile.save()
            register = True
        else:
            print user_form.errors, profile_form.errors
    else:
        user_form = Userform()
        profile_form = UserProfileForm()
    return render(
        request, "blog/register.html", {"user_form": user_form, "profile_form": profile_form, "registered": register}
    )
Exemple #14
0
def register_profile(request):
    registered = False
    if request.method == 'POST':
        profile_form = UserProfileForm(data=request.POST)

        if profile_form.is_valid():
            profile = profile_form.save(commit=False)
            profile.user = User.objects.get(username=request.user)

            if 'picture' in request.FILES:
                profile.picture = request.FILES['picture']

            profile.save()
            registered = True

        else:
            print profile_form.errors
    else:
        profile_form = UserProfileForm()

    return render(request,
            'blog/profile_registration.html',
            {'profile_form': profile_form, 'registered': registered} )
Exemple #15
0
def register(request):
    tagss = Tag.objects.all()
    posts = Post.objects.order_by('-created_date')

    for tag in tagss:
        q = Post.objects.filter(tags=tag)
        z = q.count() > 2
        if z:
            x = True
            tag.important = x
            tag.save()
    tags = Tag.objects.filter(important=True)
    # Like before, get the request's context.

    context = RequestContext(request)

    # A boolean value for telling the template whether the registration was successful.
    # Set to False initially. Code changes value to True when registration succeeds.
    registered = False

    # If it's a HTTP POST, we're interested in processing form data.
    if request.method == 'POST':

        user_form = UserForm(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)

        if user_form.is_valid() and profile_form.is_valid():

            user = user_form.save()
            user.set_password(user.password)
            user.save()
            profile = profile_form.save(commit=False)
            profile.user = user
            profile.save()
            registered = True
        else:
            print user_form.errors, profile_form.errors

    else:
        user_form = UserForm()
        profile_form = UserProfileForm()
    return render_to_response(
        'blog/register.html', {
            'user_form': user_form,
            'profile_form': profile_form,
            'registered': registered,
            'tags': tags
        }, context)
Exemple #16
0
def edit_profile(request):
    edited_profile = UserProfile.objects.get(user=request.user)
    if request.method == "POST":
        user = UserChangeForm(request.POST, instance=request.user)
        profile_form = UserProfileForm(
            data=request.POST, instance=edited_profile)
        if user.is_valid() and profile_form.is_valid():
            saved_user = user.save()
            profile = profile_form.save(commit=False)
            if 'profile_pic' in request.FILES:
                profile.profile_pic = request.FILES['profile_pic']
            profile.save()
            return HttpResponseRedirect(reverse("account:profile"))
        else:
            print(user.errors, profile_form.errors)
    else:
        user_form = UserChangeForm(instance=request.user)
        profile_form = UserProfileForm(instance=edited_profile)
        return render(request, "registration/register.html", {'user_form': user_form, 'profile_form': profile_form})
Exemple #17
0
def register(request):
    if request.method == 'POST':
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)

        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.save()
            profile = profile_form.save(commit=False)
            profile.user = user
            profile.save()

            return HttpResponseRedirect("/blog/")

    else:
        user_form = UserForm()
        profile_form = UserProfileForm()

    return render(request, 'blog/register.html', {
        'user_form': user_form,
        'profile_form': profile_form
    })
Exemple #18
0
def register(request):
    registered = False

    if request.method == 'POST':
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)

        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.save()

            profile = profile_form.save(commit=False)
            profile.user = user

            if 'profile_picture' in request.FILES:
                profile.profile_picture = request.FILES['profile_picture']

            profile.save()
            registered = True

            return HttpResponseRedirect('/blog/home/')

        else:
            print(user_form.errors, end='\n')
            print(profile_form.errors, end='\n')

    else:
        user_form = UserForm()
        profile_form = UserProfileForm()

    return render(
        request, 'register.html', {
            'user_form': user_form,
            'profile_form': profile_form,
            'registered': registered
        })