简体   繁体   English

无法弄清楚如何在项目中表达我的逻辑

[英]can't figure out how to present my logic within the project

well done. 做得好。 i have got to admit that stack overflow has helped me when all seemed dreadful. 我必须承认,当所有人看上去都很可怕时,堆栈溢出已经帮助了我。 in my simple application, i need when a person books a room, 在我的简单应用程序中,我需要一个人预订房间时

  1. it shows a confirmation message 它显示一条确认消息

  2. there is an auto decrement in book number ie let's say, if there are 40 rooms, after a single book they are decremented to 39 有一个自动递减的书号,也就是说,如果有40个房间,则在一本书之后,它们会递减到39个

  3. to make sure one person can book only once. 确保一个人只能预订一次。

    though the code i have been putting seemed wrong and irrelevant, any help given is appreciated. 尽管我所输入的代码似乎是错误且无关紧要的,但给予的任何帮助还是值得赞赏的。 is the logic put in models or views, and if yes, just show me atleast a line of code how it can be iniciated. 是模型或视图中的逻辑,如果是的话,请至少向我展示一行代码,以介绍如何使用它。

models.py models.py

class Booking(models.Model):
    Book_No = models.IntegerField(default=1)
    Hostel = models.ForeignKey(List, null=True, blank=True)
    Room_Number = models.ForeignKey(Room)
    Room_capacity = models.CharField(max_length=1, choices=[('S', 'Single'), ('D', 'Double')], default="--")
    Booked_by = models.ForeignKey(Student, default='--')
    Booked_on = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return '{}, booked a {} room in {} {} at {}'.format(
            self.Booked_by, self.Room_capacity, self.Hostel, self.Room_Number, self.Booked_on)

views.py views.py

def book(request):
    if request.method == "POST":
        form = BookForm(request.POST)
        if form.is_valid():
            post = form.save(commit=True)
            post.save()
    else:
        form = BookForm()
    return render(request, 'gyobera/book_now.html', {'form': form})

forms.py 表格

class BookForm(forms.ModelForm):
    class Meta:
        model = Booking
        fields = ('Book_No', 'Hostel', 'Room_Number', 'Room_capacity', 'Booked_by')

book_now.html book_now.html

{% extends 'base.html' %}

{% load staticfiles %}

{% block title %}{{ classification_name }}{% endblock %}

{% block body_block %}
        <h1>Book_now</h1> <a href="/Gyobera/">Home</a><br/>
<br/>
    <form method="POST" class="post-form">{% csrf_token %}
        {{ form.as_p }}
        <button type="submit" class="save btn btn-default">Book_now</button>
    </form>
{% endblock %}

thanks 谢谢

i chose to edit and post the question here since it's more similar like the one i had posted above.. i have these here: below is my view to pull the available rooms from the database to be viewed on the site and the book view. 我选择在此处编辑和发布问题,因为它与我在上面发布的问题更加相似。我在这里有这些:以下是我的观点,从数据库中提取可用房间,以便在站点和书本视图中进行查看。 in my appplication you can book a hostel but when you book it off, it doesn't show or simply put it remains in the database as an available room, the code i had written seems totally wrong and irrelevant, i seek some help please.. then what is the simplest way i can filter hostels availed to make sure if a male is booking, hostels of that gender are only availed. 在我的应用程序中,您可以预订旅馆,但是当您预订旅馆时,它不会显示或只是将其作为可用房间保留在数据库中,我编写的代码似乎完全错误且无关紧要,请寻求帮助。那么,最简​​单的方法是我可以过滤可用的旅馆,以确保是否有男性在预订,而仅使用该性别的旅馆。 i hope the code below would really work to aid help. 我希望下面的代码确实可以帮助您。 thanks 谢谢

views.py views.py

def rooms(request):
    context_dict = {}
    context = RequestContext(request)
    room_list = Room.objects.all()
    for room in room_list:
        context_dict = {'rooms': room_list}
    return render_to_response('gyobera/room.html', context_dict, context)

def book(request):
    # check if user has already booked:
    has_booked = Booking.objects.filter(Booked_by_id=request.POST.get('booked_by')).exists()
    # are room free?
    rooms_full = Booking.objects.count() == 40
    if rooms_full or has_booked:
        # redirect to a error view
        return 'You have reserved yourself a room'

    if request.method == "POST":
        form = BookForm(request.POST)
        if form.is_valid():
            post = form.save(commit=True)
            post.save()
    else:
        form = BookForm()
    return render(request, 'gyobera/book_now.html', {'form': form})

models.py models.py

class Room(models.Model):
    Hostel = models.ForeignKey(List)
    Room_Number = models.CharField(max_length=50)
    Total_rooms = models.IntegerField(default=0, blank=True)
    Price_single = models.IntegerField(default=0, blank=True)
    Price_double = models.IntegerField(default=0, blank=True)

    def __str__(self):
        return ' {}   |   {} '.format(self.Hostel, self.Room_Number)


class Booking(models.Model):
    Book_No = models.IntegerField(default=1)
    Gender = models.CharField(max_length=1, choices=[('M', 'Male'), ('F', 'Female')], default="--")
    Hostel = models.ForeignKey(List, null=True, blank=True)
    Room_Number = models.ForeignKey(Room)
    Room_capacity = models.CharField(max_length=1, choices=[('S', 'Single'), ('D', 'Double')], default="--")
    Booked_by = models.ForeignKey(Student, default='--')
    Booked_on = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return '{}, {} booked a {} room in {} {} at {}'.format(
            self.Booked_by, self.Gender, self.Room_capacity, self.Hostel, self.Room_Number, self.Booked_on)

room.html room.html

{% extends 'base.html' %}

{% load staticfiles %}

{% block title %}{{ category_name }}{% endblock %}

{% block body_block %}
    <h1><strong>Available rooms </strong></h1><a href="/Gyobera/">Home</a><br/>
          {% if rooms %}
            <ul>
                {% for room in rooms %}
                 <!-- Following line changed to add an HTML hyperlink -->
                <li><a href="{{ room_list}}">{{ room }}</a></li>
                {% endfor %}
            </ul>
        {% else %}
            <strong>There are no rooms in this hostel.</strong>
        {% endif %}
{% endblock %}

There are many ways to achieve this. 有很多方法可以实现这一目标。 The simplest is to add this in the view. 最简单的方法是在视图中添加它。 There you have to check, if the user has already booked and if there are still rooms free. 在那里,您必须检查用户是否已经预订,以及是否还有可用的房间。

def book(request):
    # check if user has already booked:
    has_booked = Booking.objects.filter(booked_by=request.user).exists()
    # are room free?
    rooms_full = Booking.objects.count() == 40
    if rooms_full or has_booked:
        # redirect to a error view
        return ...


    if request.method == "POST":
        form = BookForm(request.POST)
        if form.is_valid():
            post = form.save(commit=True)
            post.save()
    else:
        form = BookForm()
    return render(request, 'gyobera/book_now.html', {'form': form})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM