我创建了一个预订系统,用户可以进行多次预订。我正在尝试为用户创建一种编辑这些预订的方法,并创建了以下机制。
用户预订 - >在数据库中创建的对象 - >用户可以看到即将发布的模板中列出的即将到来的预订 - >用户可以更改预订。
我遇到问题的地方是我尝试访问编辑视图的时候。基本上,当单击编辑按钮时,无论预订如何,它都将始终将用户带到数据库中第一个对象的预订表单,而不是相关的对象。
见下面的代码:
models.py
class Booking(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
unique_id = models.UUIDField(unique=True, default=uuid.uuid4, editable=False)
coursename = models.CharField(max_length=200)
daterequired = models.DateTimeField(default=datetime.now(timezone.utc))
students = models.CharField(max_length=200)
length = models.IntegerField()
matches = models.ManyToManyField(Student, related_name='matches')
slug = models.SlugField(max_length=40, blank=True)
def save(self, *args, **kwargs):
if not self.pk:
self.slug = slugify(self.name)
super(Booking, self).save(*args, **kwargs)
def __str__(self):
return str(self.user.username)
views.py
def upcoming(request):
booking = Booking.objects.filter(user=request.user)
for bookings in booking:
return render(request, 'classroom/teachers/upcoming.html', {'booking':booking})
return redirect('teachers:book')
def addstudents(request, unique_id):
booking = Booking.objects.get(unique_id=unique_id)
if (request.method == 'POST'):
form = BookingForm(data=request.POST, instance=booking)
if form.is_valid():
form.save(commit=True)
return redirect('teachers:upcoming')
else:
booking_dict = model_to_dict(booking)
form = BookingForm(booking_dict)
return render(request, 'classroom/teachers/adding.html', {'form':form})
urls.py
path('teachers/', include(([
path('', teachers.QuizListView.as_view(), name='quiz_change_list'),
path('choose/', teachers.choose, name='choose'),
path('book/', teachers.book, name='book'),
path('addstudents/<uuid:unique_id>', teachers.addstudents, name='studentadd'),
path('upcoming/', teachers.upcoming, name='upcoming'),
], 'classroom'), namespace='teachers')),
upcoming.html
{% extends 'base.html' %}
{% block content %}
<div class="container">
<div class="card">
<div class="card-header">
Upcoming Bookings
<a href="{% url 'teachers:book' %}" style="float:right !important;" class="btn btn-primary btn-sm">Book More</a>
</div>
{% for bookings in booking %}
<div style="border-bottom: 1px solid rgba(0,0,0,.125);" class="card-body">
<h5 class="card-title">{{ bookings.coursename }}</h5>
<p class="card-title"><strong>Unique Course ID:</strong> {{ bookings.unique_id }}</p>
<span class="badge badge-primary badge-pill">{{ bookings.daterequired }}</span>
<span class="badge badge-primary badge-pill">{{ bookings.students }} Students.</span>
<span class="badge badge-primary badge-pill">{{ bookings.length }} Hours.</span>
<button style="float: right !important;" type="button" class="btn btn-danger" data-toggle="modal" data-target=".bd-example-modal-lg">Student List</button>
</div>
<div class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Student List</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<table class="table table-striped">
<thead>
<tr>
<th scope="col">Username</th>
</tr>
</thead>
<tbody>
{% for matches in bookings.matches.all %}
<tr>
<td>{{ matches }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<a href="{% url 'teachers:studentadd' bookings.unique_id%}"><button type="button" class="btn btn-primary">Add More Students</button></a>
</div>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
{% endblock %}
forms.py
class BookingForm(forms.ModelForm):
class Meta:
model = Booking
exclude = ['user', ]
widgets = {
'students': forms.TextInput(attrs={'placeholder': 'Number of Students'}),
'length': forms.TextInput(
attrs={'placeholder': 'Time needed in hours'}),
'coursename': forms.TextInput(
attrs={'placeholder': 'Name of your course for students'}),
}
labels = {
'daterequired': 'What date and time do you require?',
'coursename': 'Name of Upcoming Course'
}
链接结构:
AddStudents表单:
仅供参考 - 添加学生是预订表格编辑页面。
任何人都可以告诉我为什么我只能编辑数据库中的第一个对象?