def post(self, request, ownerName, pk): """ Shows a form to create a new blog :param request: HttpRequest :return: HttpRequest """ if request.user.is_superuser or request.user.username == ownerName: if request.user.is_superuser: blog_req = Blog.objects.filter(pk=pk) else: blog_req = Blog.objects.filter(pk=pk, owner=request.user) if len(blog_req) >= 1: blog = blog_req[0] else: return HttpResponseNotFound("Error 404 Not Found") else: return HttpResponseNotFound("Error 404 Not Found") form = BlogForm(request.POST, instance=blog) if form.is_valid(): new_blog = form.save() # Guarda el objeto y lo devuelve FTW return redirect('blog_detail', ownerName=new_blog.owner, pk=new_blog.pk) context = { 'form': form } return render(request, 'blogs/new_blog.html', context)
def new(request, community_id): community = get_object_or_404(Community, id=community_id) blog_form = BlogForm(request.POST or None) if blog_form.is_valid(): blog = blog_form.save(commit=False) blog.community = community blog.save() return redirect_to_view("blog", id=blog.id) return {"community": community, "blog_form": blog_form}
def post(self, request): """ Shows a form to create a new blog :param request: HttpRequest :return: HttpRequest """ blog_with_owner = Blog() blog_with_owner.owner = request.user form = BlogForm(request.POST, instance=blog_with_owner) if form.is_valid(): new_blog = form.save() # Guarda el objeto y lo devuelve FTW return redirect('blog_detail', ownerName=new_blog.owner, pk=new_blog.pk) context = { 'form': form } return render(request, 'blogs/new_blog.html', context)
def get(self, request): """ Presenta el formulario de creación de un blog :param request: objeto HttpRequest :return: HttpResponse con respuesta """ form = BlogForm() context = {'form': form} return render(request, 'blogs/form.html', context)
def get(self, request): """ Muestra el formulario para crear un blog :param request: objeto HttpRequest :return: HttpResponse con la respuesta """ form = BlogForm() context = {'form': form} context['title'] = "Create a new Blog" context['claim'] = 'Create an incredible new blog' return render(request, 'blogs/new-blog.html', context)
def post(self, request): """ Recibe los datos del nuevo post los valida y los guarda :param request: HttpRequest :return: HttpResponse """ # crear el formulario con los datos del POST blog_with_user = Blog(owner=request.user) form = BlogForm(request.POST, instance=blog_with_user) if form.is_valid(): #crea el post blog = form.save() #generar mensaje de exito msg = "Blog creado con éxito" # limpiamos el formulario creando uno vacío para pasar a la plantilla form = BlogForm() else: msg = "Ha ocurrido un error al guardar el blog" \ # renderiza la plantilla con el formulario context = {"form": form, "msg": msg} # renderiza y devuelve la plantilla return render(request, 'blogs/new-blog.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)
def get(self, request): """ Sireve el formulario de crear post al usuario :param request: HttpRequest :return: HttpResponse """ # crear el formulario form = BlogForm() # renderiza la plantilla con el formulario context = {"form": form} # renderiza y devuelve la plantilla return render(request, 'blogs/new-blog.html', context)
def post(self, request): blog_with_user = Blog(owner=request.user) form = BlogForm(request.POST, instance=blog_with_user) self.translate(form) message = "" if form.is_valid(): form.save() form = BlogForm() message = _("Se ha creado correctamente el blog") context = {"form": form, "message": message} return render(request, 'blogs/new_blog.html', context)
def post(self, request): new_blog = Blog(author=request.user) form = BlogForm(request.POST, instance=new_blog) if form.is_valid(): new_blog = form.save() messages.success( request, 'Blog {0} created successfully!'.format(new_blog.name)) form = BlogForm() return render(request, 'blogs/blog_new.html', {'form': form})
def post(self, request): blog = Blog() blog.owner = request.user form = BlogForm(request.POST, instance=blog) if form.is_valid(): new_blog = form.save() messages.success( request, 'Post creado correctamente con ID {0}'.format(new_blog.pk)) form = BlogForm() context = {'form': form} return render(request, 'new-post.html', context)
def post(self, request): new_blog = Blog() blog_form = BlogForm(request.user, request.POST, request.FILES, instance=new_blog) if blog_form.is_valid(): new_blog = blog_form.save() messages.success( request, 'El blog {0} se ha creado corretamente!'.format( new_blog.title)) blog_form = BlogForm(request.user) return render(request, 'new_blog.html', {'form': blog_form})
def get(self, request, ownerName, pk): """ Shows a form to create a new blog :param request: HttpRequest :return: HttpRequest """ if request.user.is_superuser or request.user.username == ownerName: if request.user.is_superuser: blog_req = Blog.objects.filter(pk=pk) else: blog_req = Blog.objects.filter(pk=pk, owner=request.user) if len(blog_req) >= 1: blog = blog_req[0] else: return HttpResponseNotFound("Error 404 Not Found") form = BlogForm(instance=blog) context = {'form': form} return render(request, 'blogs/new_blog.html', context) else: return HttpResponseNotFound("Error 404 Not Found")
def post(self, request): # Nos definimos previamente un formulario con el valor del usuario que hemos excluido de la página. new_blog_in = Blog(owner=request.user) # Coge los datos del formulario y de los ficheros form = BlogForm(request.POST, instance=new_blog_in) if form.is_valid(): new_blog_in = form.save() messages.success( request, 'Blog {0} creado con éxito!'.format(new_blog_in.title)) # Se limpia el formulario para una nueva inserción form = BlogForm() else: storage = generate_new_html_errors(form.errors) for msg in storage: messages.error(request, storage.get(msg)) return render(request, 'blogs/new_blog.html', {'form': form})
def post(self, request): """ Procesa el formulario para crear un blog :param request: objeto HttpRequest :return: HttpResponse con la respuesta """ blog = Blog() blog.author = request.user form = BlogForm(request.POST, instance=blog) if form.is_valid(): # creamos el blog blog = form.save() # limpiar el formulario form = BlogForm() # Devolvemos un mensaje de OK messages.success(request, 'Blog created correctly') return redirect('blogs') context = {'form': form} context['title'] = "Create a new Blog" context['claim'] = 'Create an incredible new blog' messages.error(request,'Blog could not be created. Please check fields') return render(request, 'blogs/new-blog.html', context)
def post(self, request): """ Procesa el formulario de creación de un blog :param request: objeto HttpRequest :return: HttpResponse con respuesta """ blog = Blog() try: blog.owner = User.objects.get(username=request.user) except Blog.DoesNotExist: return HttpResponse('Este usuario no existe', status=404) form = BlogForm(request.POST, request.FILES, instance=blog) if form.is_valid(): new_blog = form.save() form = BlogForm() messages.success(request, 'Blog creado correctamente') context = {'form': form} return render(request, 'blogs/form.html', context)
def get(self, request): form = BlogForm() context = {'form': form} return render(request, 'blogs/new_blog.html', context)
def get(self, request): blog_form = BlogForm(request.user) return render(request, 'new_blog.html', {'form': blog_form})
def get(self, request): form = BlogForm() self.translate(form) context = {"form": form} return render(request, 'blogs/new_blog.html', context)
def get(self, request): form = BlogForm() context = {'form': form} return render(request, 'new-post.html', context)
def dashboard(request): try: blog = Blog.objects.get(user=request.user) if not resolve_subdomain(request.META['HTTP_HOST'], blog): return redirect(f"http://{get_root(blog.subdomain)}/dashboard") if request.method == "POST": form = BlogForm(request.POST, instance=blog) if form.is_valid(): blog_info = form.save(commit=False) blog_info.save() else: form = BlogForm(instance=blog) return render(request, 'dashboard/dashboard.html', { 'form': form, 'blog': blog, 'root': get_root(blog.subdomain) }) except Blog.DoesNotExist: if request.method == "POST": form = BlogForm(request.POST) if form.is_valid(): blog = form.save(commit=False) blog.user = request.user blog.created_date = timezone.now() blog.save() return render(request, 'dashboard/dashboard.html', { 'form': form, 'blog': blog, 'root': get_root(blog.subdomain), }) return render(request, 'dashboard/dashboard.html', {'form': form}) else: form = BlogForm() return render(request, 'dashboard/dashboard.html', {'form': form})
def get(self, request): form = BlogForm() return render(request, 'blogs/blog_new.html', {'form': form})