Justin
Justin

Reputation: 1479

Alert user in template after they have successfully created/edited/deleted a post(object)

Summary: I am trying to create a job board website. I have a dashboard view that returns all job objects and displays them in a template. The user can navigate here just to view their posts and the user is also redirected here after creating a new post, editing a post or deleting a post. I would like to be able to alert the user "Your post was created/edited/deleted successfully" depending on the circumstance of how they got to this page, but am not sure the best way to go about. Below is how I implemented the functionality to alert the user a post has been created, but I don't think it was the best way of doing this.

The first view I created was the post_job view where a user can create a new job post. To mark if a job was new I thought to add a boolean field to the Job model:

class Job(models.model):
    #...
    new = models.BooleanField(default = True) # post is new by default, set to False later

and then do this in the dashboard :

@login_required
def dashboard(request):
    jobs = Job.objects.all()
    new_job = False # set to true if there is a new job ( would only be the case if the user got directed to this view after posting a job)
    for job in jobs: # loop through jobs to see if any have new=True
        if job.new:
            new_job = True
            job.new = False # set to false so it's not considered new next time dashboard is loaded
            job.save()
    return render(request, 'job/dashboard.html', {'jobs':jobs, 'new_job': new_job})

and in dashboard.html:

    {% if new_job %}
        <p>Your job was posted successfully</p>
    {% endif %}

This works and successfully alerts the user only when they have just created a new post. However, I feel like there has to be a better way to implement this functionality then adding an edited field to the Job model and was about to say a deleted field but I guess the object wouldn't exist anymore. Anyway, if you can suggest anything to achieve this thanks for the help. Not sure if it's the right term but it seems like there would be a flagging system that alerts when an object is created/edited/updated being that this is fairly common?

Edit: Is there a way to pass additional variables when redirecting to the dashboard view? For instance here is the post_job view:

def post_job(request):
    if request.method == 'POST':
        form = JobForm(request.POST)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.business= Business.objects.get(user=request.user)
            instance.save()
            return redirect('dashboard') # is there a way to tell the dashboard view the post_job view sent the user? 
    else:
        form = JobForm()
    return render(request, 'job/post_job.html', {'section': 'dashboard', 'form':form})

if there were I could just do this for the edit and delete views.

Upvotes: 0

Views: 496

Answers (1)

ketcham
ketcham

Reputation: 932

Django's messages framework is built for use cases exactly like this. Instead of having

new_job = True

before calling .save(), use:

messages.success(request, 'Job successfully created/updated/deleted.')

Then, in your html file, you'll have:

     {% if messages %}
        <ul class="messages">
            {% for message in messages %}
                <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
            {% endfor %}
        </ul>
    {% endif %}

Also, I would suggest running some sort of clean/validation before calling .save(). If that validation fails, you can create an error or warning message to display. Documentation on the Django messages framework can be found here.

Upvotes: 1

Related Questions