Django - get data from form and return to user so they can edit/delete

79
April 12, 2022, at 02:10 AM

New to Django and making a restuarant booking system. I have everything going to my database but now i'm trying to return the info back to the user, ideally in a similar form view to what i have, so they can edit or delete the booking.

My code so far is as follows:

models.py:

class Reservations(models.Model):
    reservation form categories and attributes
    name = models.CharField(max_length=50)
    phone_number = models.CharField(validators=[phoneNumberRegex], max_length=16, 
unique=True)
    email = models.EmailField()
    date = models.DateField()
    time = models.CharField(choices=time_options, default="12pm", max_length=10)
    number_of_party = models.IntegerField(choices=party_size, default=1)
    reservation_id = models.UUIDField(primary_key=True, default=uuid.uuid4, 
editable=False, max_length=15)

    class Meta:
        ordering = ['date']
        verbose_name = 'Reservation'
        verbose_name_plural = 'Reservations'
    def __str__(self):
        return self.name

Then my form looks like this:

form.py:

class ReservationForm(forms.ModelForm):
    class Meta:
        model = Reservations
        fields = ['name', 'phone_number', 'email', 'date', 'time', 'number_of_party']
        widgets = {
            'date': DatePickerInput(format='%d-%m-%Y'),
        }

Then where I think i'm really getting lost is with the views.

view.py:

class ReservationsFormView(CreateView):
    model = Reservations
    template_name = "reservations/reservations.html"
    form_class = ReservationForm
    success_url = "reservation_complete/"

    def form_valid(self, form):
        return super().form_valid(form)

class EditReservationView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
    model = Reservations
    template_name = "reservations/edit_reservations.html"
    form_class = ReservationForm
    success_url = "reservation_complete/"
    def test_func(self):
        return self.request.user == self.get_object().user

class ReservationCompleteView(CreateView):
    template_name = "reservations/reservation_complete.html"
    success_url = "/reservation_complete/"
    form_class = ReservationForm
    model = Reservations

class ReservationAccountView(TemplateView):
    template_name = "reservations/reservations_account.html"
class DeleteReservationView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
    """ A view to delete an reservation """
    model = Reservations
    success_url = "/reservations/"
    def test_func(self):
        return self.request.user == self.get_object().user

So basically if someone could help me extract the data and present back to the user in a html file I would greatly appreciate it. A lot of the material online isn't using class based views but i start that way so sticking it out. Sorry for the verbose code. Thanks in advance

Answer 1

While Django's Edit, Create, etc views are quick and helpful, I find them hard to manipulate, especially if I haven't used them in a while. I recommend getting used to writing your own views, that way you can set whatever action you want the view to perform.

Let's say your URL looks like this - edit/4281ee81-a12a-4a0f-8928-2d9639903bd7/

forms.py

class EditReservationForm(forms.Form):
    name = forms.CharField()
    email = forms.EmailField()

views.py

class EditReservationView(LoginRequiredMixing, View):
    def get(self, request, *args **kwargs):
        reservation = Reservations.objects.get(uuid=kwargs['uuid'])
        form = EditReservationForm(initial={"name": reservation.name, "email": reservation.email})
        return render(request, "reservation/edit-reservation.html", {"form": form})

You now have a bound form to data from a specific reservation. I hope this helps. And more info on initial data here - initial form values

Rent Charter Buses Company
READ ALSO
For python's built-in sort()'s key parameter, when to use base class vs object as identifier?

For python's built-in sort()'s key parameter, when to use base class vs object as identifier?

Let me take an example to clarify what I am talking aboutSay I'm trying to sort() a string s with a key of count (frequency of letters) and in a different case, with a key of lower

87
Get cpu temperature from python? (windows)

Get cpu temperature from python? (windows)

PleaseI have tried every google search result and none of them worked

128
How to go through a Django dictionary in HTML template

How to go through a Django dictionary in HTML template

Im new to Django and I don't really have a clear idea how the Django Template language works but I have a dictionary that is something like this:

106