0

How can I set my function in views.py so that a form redirects to the same page after submitting?

For example, I want to add time slots:

In my views.py:

@login_required
def post_available_timeslots(request):
    print("Check")
    if not check_existing_timeslot(request):
        print("Time slot does not exist")
        instance = TimeSlot()
        data = request.POST.copy()
        data["time_slot_owner"] = request.user
        # data["food_avail_id"] = FoodAvail.objects.get(author=request.user).pk
        form = TimeSlotForm(data or None, instance=instance)
        if request.POST and form.is_valid():
            form.save()
            return redirect("food_avail:view_food_avail_res")
    else:
        print("Time slot does not exist")
        messages.info(request, "Time Slot Already Exists")
    return render(request, "food_avail/view_food.html", {"time_slot": form})

in models.py

class TimeSlot(models.Model):
    time_slot_owner = models.ForeignKey(User, on_delete=models.CASCADE)
    # food_avail_id = models.ForeignKey(FoodAvail, on_delete=models.CASCADE)
    start_time = models.TimeField()
    end_time = models.TimeField()

    def __str__(self):
        return str(self.start_time) + "-" + str(self.end_time)

In forms.py: class TimeSlotForm(ModelForm):

class Meta:
    model = TimeSlot
    fields = ["start_time", "end_time"]

In urls.py:

from django.urls import path
from food_avail import views

app_name = "food_avail"

urlpatterns = [
    path("post_food_avail/", views.post_available_food, name="post_food_avail"),
    # path("post_food_avail/", views.FoodAvailCreateView.as_view(), name="post_food_avail"),
    # path("update_food_avail/", views.post_available_food, name="update_food_avail"),
    path("food_avail_all/", views.check_food_availibility, name="view_food_avail"),
    path("food_avail_res/", views.view_available_food, name="view_food_avail_res"),
    # path("food_avail_res/",  views.post_available_timeslots, name="create_timeslots"),

]

in my html template:

{% if form.errors %}
    {% for field in form %}
        {% for error in field.errors %}
            <div class="alert alert-danger">
                {{ field.label }} <strong>{{ error|escape }}</strong>
            </div>
        {% endfor %}
    {% endfor %}
    {% for error in form.non_field_errors %}
        <div class="alert alert-danger">
            {{ field.label }} <strong>{{ error|escape }}</strong>
        </div>
    {% endfor %}
{% endif %}
{% if user.is_authenticated %}
<div>
  <div>
    <div>
      <div>
        <h4>
          {{ food.author.username }}<br>
        </h4>
        </div>
        <div>
          Food Available: {{ food.food_available }}<br>  
          Description: {{ food.description }}<br>
          {% endif %}
        </div>
    </div>

    <div>
        <h4>Add Time Slot</h4>
        <br><br>
    </div>
    <div>
        <div>
            <form method="post">
                 {{ time_slot.as_p }}
                <button type="submit" class="button btn-success">Submit</button>
            </form>
        </div>

    
        {% if time_slot %}
        <h4> List Of Time Slots </h4>
              </div>
              <div class="card-body">
                  
                <div class="table-responsive">
                  <table class="table table-hover ">
                    <thead>
                      <tr>
                        <th>Start Time</th>
                        <th>End Time</th>
                          <th>Edit</th>
                          <th>Delete</th>
                      </tr>
                    </thead>
                    <tbody>
                        {% for x in time_slot %}
                          <tr>
                              <td>{{x.start_time | time:'H:i:s'}}</td>
                              <td>{{x.end_time | time:'H:i:s'}}</td>
                              <td>
                                  {% endfor %}
                    {% endif %}

I have been stuck on this problem for a minute now so any help would be much appreciated. This is how it currently shows up on my HTML template:

Sumithran
  • 6,217
  • 4
  • 40
  • 54
ailsin
  • 35
  • 6
  • Either don't redirect because it should not be needed on same page posts, a POST is a new request...or use: `request.META.get('HTTP_REFERER')` to redirect to the last referred page as shown here: https://stackoverflow.com/q/4406377/8382028 – ViaTech Nov 18 '21 at 02:10
  • so I tried both ways, I removed the redirect and it still shows the same thing and I also tried adding redirect(request.META.HTTP_REFERER) but it shows the same thing for that as well. I realized by my print statement that when I hit submit, django is not even looking into the post_available_timeslots function – ailsin Nov 18 '21 at 02:18
  • no path in your url patterns points to post_available_timeslots – AMG Nov 18 '21 at 03:01
  • hi @AMG, how can I address that? I was confused because how can I have two views going to the same url? – ailsin Nov 18 '21 at 03:04
  • 1
    post your code for the view_available_food. You probably want to be posting back to it. In there you'd have a `if request.method == "POST"` and do your form save there. It isn't clear currently which url path is rendering your html shown. – AMG Nov 18 '21 at 04:20
  • hey @AMG, thank you so much! That was indeed what it was, it is working now after i added the request.method == "POST" to views_available_food – ailsin Nov 18 '21 at 04:47

0 Answers0