def post(self, req): error_messages = [] success_message = '' user = User() form = UserCreateForm(req.POST) if form.is_valid(): new_user = form.save() new_user.set_password(new_user.password) new_user.save() form = UserCreateForm() success_message = u'User creado con éxito! ' # blog for this user. blog = Blog(owner=new_user) if not req.POST.get('blog_name'): blog.name = 'Blog de ' + new_user.username else: blog.name = req.POST.get('blog_name') if not req.POST.get('blog_sdescription'): blog.short_description = 'Bienvenido al blog de ' + new_user.username else: blog.short_description = req.POST.get('blog_sdescription') blog.save() else: error_messages.append(u'Formulario incompleto.') context = { 'form': form, 'success_message': success_message, 'error_messages': error_messages } return render(req, 'users/signup.html', context)
def post(self,request): user = User() form = UserForm(data=request.POST) form.instance = user if form.is_valid(): user.first_name = form.cleaned_data.get('first_name') user.last_name = form.cleaned_data.get('last_name') user.email = form.cleaned_data.get('email') user.username = form.cleaned_data.get('username') password = form.cleaned_data.get('password') user.set_password(password) user.save() blog = Blog(user=user) blog.name = form.cleaned_data.get('blog_name') blog.description = form.cleaned_data.get('blog_description') blog.save() # logeamos al usaurio authenticate_user = authenticate(username=user.username, password=password) django_login(request, authenticate_user) # vaciamos el formulario form = UserForm() url = reverse("create_post") message = "User with blog created successfully!" message += '<a href="{0}">Create you first post</a>'.format(url) # enviamos mensaje de exito con un enlace al blog que acabamos de crear messages.success(request, message) return render(request, "singup_form.html", {'form': form})
def post(self, request): error_messages = [] form = SignupForm(request.POST) if form.is_valid(): first_name = form.cleaned_data.get("first_name") last_name = form.cleaned_data.get("last_name") username = form.cleaned_data.get("usr") email = form.cleaned_data.get("email") password = form.cleaned_data.get("pwd") blog_name = form.cleaned_data.get("blog_name") users = User.objects.filter(username=username) if len(users) == 0: new_user = User() new_user.username = username new_user.first_name = first_name new_user.last_name = last_name new_user.email = email new_user.set_password(password) new_user.save() blog = Blog() blog.name = blog_name blog.author = new_user blog.save() return redirect("posts_home") else: error_messages.append("El username {0} ya existe. Pruebe con otro".format(username)) context = {"errors": error_messages, "signup_form": form} return render(request, "users/signup.html", context)
def post(self, request): form = SignupForm(request.POST) self.translate(form) context = dict() if form.is_valid(): if User.objects.filter( username=form.cleaned_data.get("username")).exists(): form.add_error("username", _("El usuario ya existe")) else: user = User() user.username = form.cleaned_data.get("username") user.set_password(form.cleaned_data.get("password")) user.first_name = form.cleaned_data.get("first_name") user.last_name = form.cleaned_data.get("last_name") user.email = form.cleaned_data.get("email") try: user.save() blog = Blog() if user.first_name or user.last_name: blog.name = "Blog de " + user.first_name + " " + user.last_name else: blog.name = "Blog de " + user.username blog.owner = user blog.save() url = request.GET.get('next', '/new-post') return redirect(url) except Error as err: form.add_error("__all__", _("Error en acceso a base de datos")) print("Error en acceso a base de datos: ", err) context["form"] = form return render(request, 'signup.html', context)
def post(self, request): serializer = UserSerializer(data=request.data) # se inicializa el serializer con los datos que me llegan por http mediante POST if serializer.is_valid(): new_user = serializer.save() # Guarda el objeto en BB.DD. y me lo devuelve new_blog = Blog() new_blog.name = 'El blog de {0} {1}'.format(request.data.get('first_name'), request.data.get('last_name')) new_blog.author = new_user new_blog.save() # serializer almacena los datos en 'serializer.data' return Response(serializer.data, status=status.HTTP_201_CREATED) else: # serializer almacena los errores en 'serializer.errors' return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def create(self, request): serializer = UserSerializer(data=request.data) #TODOS LOS MÉTODOS REST VIENEN POR EL PARAMETRO 'DATA' DE LA REQUEST if serializer.is_valid(): new_user = serializer.save() # TODO - crear el usuario y su blog en una sóla llamada blog_for_newUser = Blog() blog_for_newUser.owner = new_user blog_for_newUser.name = new_user.first_name + ' ' + new_user.last_name + '\'s Personal Blog' blog_for_newUser.save() return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): form = RegistrationForm(request.POST) if form.is_valid(): new_user = form.save() # TODO - crear el usuario y su blog en una sóla llamada blog_for_newUser = Blog() blog_for_newUser.owner = new_user blog_for_newUser.name = new_user.first_name + ' ' +new_user.last_name + '\'s Personal Blog' blog_for_newUser.save() return redirect(reverse('users_login')) else: context = { 'signup_form': form } return render(request, 'users/sign-up.html', context)
def post(self, request): form = BlogForm(request.POST) if form.is_valid(): blog = Blog() cleaned_data = form.cleaned_data blog.name = cleaned_data.get('name') blog.user = request.user blog.slug = cleaned_data.get('name').replace(" ", "_") blog.save() message = "Post creado con éxito!" form = BlogForm() else: message = "Se ha producido un error" context = {"form": form, "message": message} return render(request, 'blogs/new_post.html', context)