alioguzhan
alioguzhan

Reputation: 7917

django - how to create a preview view

i have an add page to create a new post.and i want to add a link(ahref) to preview the post at the moment. i have one form and one submit button to save the post into db. i should use the same form to preview. when i click 'preview' link the page must redirect to 'preview.html' which i can display values of form in.

i am stuck. i cannot create the algorhytm for this in my mind. there is one page.one form. one view(addPost) . and i need to reach the values of this form by another view which has another template file.

and i have two fields in models py , called 'titlepreview' and 'bodyPreview'. to see values of form in preview page ; form datas should be written into these two fields.

here models.py:

class Post(models.Model):
    owner = models.ForeignKey(User)
    title = models.CharField(max_length = 100)
    body = models.TextField()
    bodyPreview = models.TextField() #preview 
    titlePreview = models.CharField(max_length=100) # preview 
    slug = AutoSlugField(populate_from='title',unique=True)
    posted = models.DateField(auto_now_add=True)
    isdraft = models.BooleanField(default=False)

here is my add_post view:

@login_required(login_url='/login/')
def add_post(request):
    if request.method=="POST":
        form = addForm(request.POST)
        if form.is_valid():
            titleform=form.cleaned_data['title']
            bodyform=form.cleaned_data['body']
            checkform=form.cleaned_data['isdraft']
            owner = request.user
            n = Post(title = titleform, body = bodyform, isdraft=checkform, owner=owner)
            n.save()
            return HttpResponseRedirect('/admin/')

    else:
        form=addForm()
        return render(request,'add.html',{'form':form,})
    return render_to_response('add.html',{'form':form,},context_instance=RequestContext(request))

my addForm form :

class addForm(forms.Form):
    title = forms.CharField(max_length=100,widget=forms.TextInput(attrs={'placeholder':'Buraya Başlık Gelecek',}))
    body = forms.CharField(widget=forms.Textarea(attrs={'placeholder':'Buraya Metin Gelecek','rows':'25','cols':'90',}))
    isdraft = forms.BooleanField(required=False)
    #ispreview = forms.BooleanField(required=False) i just added this line as first step. :)

if another code needed ; you can comment below

thank you

Upvotes: 2

Views: 4919

Answers (1)

girasquid
girasquid

Reputation: 15526

Convert your addForm to a modelForm, and then add a submit button to your add.html template with the name '_preview' (make sure your other submit button is named '_save'). The code would look something like this:

class addForm(forms.ModelForm):
    class Meta:
        model = Post

@login_required(login_url='/login/')
def add_post(request):
    post = None
    template_name = 'add.html'
    if request.method == 'POST':
        form = addForm(request.POST)
        if form.is_valid():
          if '_preview' in request.POST:
              # don't save the post
              post = form.save(commit=False)
              template_name = 'preview.html'
          elif '_save' in request.POST:
              # save the post
              post = form.save()
              return HttpResponseRedirect('/admin/')
    else:
        form = addForm()
    return render_to_response(template_name, {'form': form, 'post': post}, context_instance=RequestContext(request))

Your template would have something like this at the bottom:

<input type='submit' name='_save' value='Save Post' />
<input type='submit' name='_preview' value='Preview Post' />

By doing it this way, you can let the user preview their post without saving it to the database - just make sure that on preview.html, you embed the form and include a save button so that they can save the post if they like what they see.

Upvotes: 1

Related Questions