问题
I am trying to update the Bar Information for users by getting their details and setting the Bar instance. But every time I click on the link on my dashboard it redirects me to the dashboard which is what I used for the else statement if it is not a post method.
view.py
def UpdateUserBar(request):
user = request.user.id
bar = Bar.objects.get(user_id=user)
form = UpdateBar(instance=bar)
if request.method == 'POST':
form = UpdateBar(request.POST,request.FILES, instance=bar)
if form.is_valid():
form.save()
return redirect('/updatebar')
messages.success(request, 'Bar Information Updated successfully')
else:
return redirect('/dashboard')
messages.error(request, 'Only Post method is accepted')
else:
form = UpdateBar(instance=bar)
context = {"form":form, "bar":bar}
return render(request, "dashboard/super/landlord/update_bar.html", context)
forms.py
class UpdateBar(ModelForm):
class Meta:
model = Bar
fields = '__all__'
models.py
class Bar(models.Model):
status = (
("open", "open"),
("closed", "closed"),
("pending", "pending"),
)
user_id = models.OneToOneField(User, on_delete=models.CASCADE)
name = models.CharField(max_length=50)
address = models.CharField(max_length=100)
opening_time = models.TimeField()
closing_time = models.TimeField()
status = models.CharField(choices=status, default="pending", max_length=14)
image = models.ImageField(upload_to='images/bars', default='images/bars/default.jpg')
def __str__(self):
return self.name
updatebar.html
{% load i18n widget_tweaks %}
<form class="form-horizontal" role="form" method="post" enctype="multipart/form-data">
{% csrf_token %}
<div class="form-group">
<label class="col-md-2 control-label">{{ form.name.label }}:</label>
<div class="col-md-10">
<input type="text" id="id_name" name="name" class="form-control" value="{{user.name}}" disabled>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">{{ form.user_id.label }}:</label>
<div class="col-md-10">
<input id="id_user_id" name="user_id" type="text" class="form-control" value="{{user_email}}" disabled>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">{{ form.address.label }}:</label>
<div class="col-md-10">
{{ form.address|attr:"class:form-control" }}
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">{{ form.opening_time.label }}</label>
<div class="col-md-10">
{{ form.opening_time | attr:"class:form-control" }}
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">{{ form.closing_time.label }}</label>
<div class="col-md-10">
{{ form.closing_time | attr:"class:form-control" }}
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">{{ form.status.label }}</label>
<div class="col-md-10">
{{ form.status| attr:"class:form-control" }}
</div>
</div>
<div class=" form-group ">
<div class="input-group col-lg-12 ">
<label class="col-md-2 control-label">{{ form.image.label}}</label>
<div class="col-md-10">
<div class="bootstrap-timepicker">
{{ form.image| attr:"class:form-control" }} </div>
</div><!-- input-group --> </div>
</div>
<div class="form-group">
<div class="col-md-10">
<center><button class="btn btn-primary" type="submit">Update Bar Info</button></center>
</div>
</div>
</form>
Every time I try to access the visit the URL, it redirects me to the dashboard.
回答1:
Alright so there's a couple of small problems here, first of all if you want to retreive only one object use get
instead of filter
because it will give you a DoesNotExist
error if no object is found, when using filter
will return an empty queryset which will make an error harder to trace.
Also in your Bar
model I see user_id
field which I presume is the owner of the Bar
, you should name it something like owner
or user
for better readability.
Here is how I would write this view:
def update_user_bar(request): # use uppercase view names for class-based-views, if its a custom view, use regular variable names
user = request.user # <------- change this
user_email = request.user.email
bar = Bar.objects.get(user_id=user)
if request.method == 'POST':
form = UpdateBar(request.POST, request.FILES, instance=bar)
if form.is_valid():
form.save()
return redirect('/updatebar')
messages.success(request, 'Bar Information Updated successfully')
else: # here is where your problem is, you need to fix your code indentation
return redirect('/dashboard')
messages.error(request, 'Only Post method is accepted')
else:
form = UpdateBar(instance=bar) # assign your form here for better radability
context = {"form":form, "user_email":user_email, "bar":bar}
return render(request, "dashboard/super/landlord/update_bar.html", context)
Update
Also Only Post method is accepted message makes no sense because to access any view in your website you are using Http GET
method before anything else :)
来源:https://stackoverflow.com/questions/65311760/not-able-to-update-an-item-in-django