簡體   English   中英

將用戶輸入從一個頁面轉移到另一個頁面

[英]Transferring user input from one page to another

我正在制作一個網站,讓學生可以為他們的課程找到即將到來的學習課程。 我在 Django 和 HTML 中這樣做。 學生將他們的課程上傳到該站點,它們在課程頁面上顯示為按鈕(例如 CS 101 - CS 簡介)。 當學生點擊他們的一門課程(按鈕)時,應該會將他們帶到一個頁面,該頁面顯示該課程的可用學習課程。 我被卡住了,因為我不知道如何根據單擊的課程正確過濾下一頁上的可用學習課程。 有沒有辦法將課程信息存儲為變量,以便在單擊按鈕時可以使用該變量來過濾結果? 編輯:我已經進行了這些更改,現在我得到一個 ValueError 太多的值,無法解壓預期的 2。我幾乎可以肯定它正在我的觀點中發生。

這是顯示用戶課程的頁面:

<div class="container h-100" style="top:50%; bottom:50%; width:100%;">
  <div class="row">
    {% if courses_list %}
    {% for course in courses_list %}
    <a type="button" class="btn btn-outline-secondary" href="{% url 'study:course-session'%}" >{{ course.subject }} {{ course.number}}-{{course.name}} </a>
    <br><br><br>
    {% endfor %}
    {% else %}
    <p class="text-center">You have not added any courses yet!</p>
    {% endif %}

  </div>
</div>

這是我試圖過濾學習課程列表的頁面(其中有一個實地課程是課程模型的外鍵):

     <h1><center>Upcoming Study Sessions</center></h1>

<div>
    <a class="btn btn-success" style="position:absolute; margin-right:2px; top:15%; right:0;" href="{% url 'study:courses' %}" role="button" >Back to My Courses</a>
  
  
</div>
<br><br>

<div class="container h-100" style="top:50%; bottom:50%; width:100%;">
    <div class="row">
      <button type="button" class="btn btn-outline-secondary" >Date/Time: {{ session.date }} <br> Location: {{ session.location }} </button>
      <br><br><br>
  
    </div>
</div>

查看模板:

def CourseSessionView(request, course_pk):

course_wanted = Course.objects.get(id=course_pk)
try:
    return Study.objects.filter(course=course_wanted)
except:
    return messages.error(request, 'There are no upcoming study sessions at this time for the requested course.')

課程和會話模型:

class Course(models.Model):
    SUBJECT_CHOICES = [
        ('AAS', 'AAS')
    ]
    subject = models.CharField(
        max_length=4, choices=SUBJECT_CHOICES, default='')
    number = models.PositiveSmallIntegerField(
        validators=[MaxValueValidator(9999)], default=0)
    name = models.CharField(max_length=100, default='')
    roster = models.ManyToManyField(
        Student, blank=True, related_name="courses")
    # Use [Student object].courses.all() to see all of a student's courses

    def __str__(self):
        return f"{self.subject} {self.number} - {self.name}"


class Study(models.Model):
    organizer = models.ForeignKey(Student, on_delete=models.CASCADE)
    date = models.DateTimeField()
    # Use [Student object].studies.all() to see all of a student's study sessions
    attendees = models.ManyToManyField(Student, related_name="studies")
    location = models.CharField(max_length=30)
    course = models.ForeignKey(Course, on_delete=models.CASCADE)

    def __str__(self):
        return f"{self.date} - {self.location}"

網址:

path('<int:course_pk>/sessions/',
         views.CourseSessionView, name='course-session')

這將是一種非常籠統的答案,因為您沒有提供模型或視圖,但我認為這個想法如下。

首先,在您的模板中,您可以在 url 中傳遞課程編號的參數:

your_template.html

<a class="btn btn-outline-secondary" 
   href="{% url 'study:course-session' course.pk %}">
   {{ course.subject }} {{ course.number}}-{{course.name}}
</a>

然后在您看來,您可以訪問該值,並從中獲取課程:

視圖.py

def the_view_name(request, course_pk):
    # Here you now have access to the course's primary key, pk, so you can get the 
    # course and filter the study sessions by that course, etc...

您需要修改 urls.py 以便視圖可以接受這個新參數:

網址.py

path('the_view_name/<int:course_pk>', views.the_view_name, name='the_view_name'),

編輯
進行以下更改:
首先到你的views.py:

def CourseSessionView(request, course_pk):

    try:
        course_wanted = Course.objects.get(id=course_pk)
    except:
        return messages.error(request, 'course not found')

    study_sessions = Study.objects.filter(course=course_wanted)

    if study_sessions.count() < 1:
        return messages.error(request, 'There are no upcoming study sessions at this time for the requested course')

    context = {
        'study_sessions': study_sessions,
    }

    return render(request, 'study/your_template_file.html', context)

然后在你的html中

<h1><center>Upcoming Study Sessions</center></h1>

<div>
<a class="btn btn-success" style="position:absolute; margin-right:2px; top:15%; right:0;" href="{% url 'study:courses' %}" role="button" >Back to My Courses</a>

</div>
<br><br>

<div class="container h-100" style="top:50%; bottom:50%; width:100%;">

  {% for session in study_sessions %}
  <div class="row">
   <button type="button" class="btn btn-outline-secondary" >Date/Time: {{ session.date }} <br> Location: {{ session.location }} </button>
   <br><br><br>
  </div>
  {% endfor %}
  
</div>

Note:基於函數的視圖的名稱不需要像您的情況那樣在PascalCase中,它應該在snake_case中。

顯示用戶課程的頁面,您需要pk的課程:


<div class="container h-100" style="top:50%; bottom:50%; width:100%;">
  <div class="row">
    {% if courses_list %}
    {% for course in courses_list %}
    <a type="button" class="btn btn-outline-secondary" href="{% url 'study:course-session' course.pk %}" >{{ course.subject }} {{ course.number}}-{{course.name}} </a>
    <br><br><br>
    {% endfor %}
    {% else %}
    <p class="text-center">You have not added any courses yet!</p>
    {% endif %}

  </div>
</div>

您對模板的看法,我在snake_case中定義它,因為它是推薦的方式。

def course_session(request, course_pk):
    course_wanted = Course.objects.get(id=course_pk)
    study_courses=''
    try:
        study_courses= Study.objects.filter(course=course_wanted)
    except:
        messages.error(request, 'There are no upcoming study sessions at this time for the requested course.')
    else:
        return render(request,'anyfolder/anyfile.html',{'study_courses':study_courses})
    
    return render(request,'anyfolder/anyfile.html') #then it will show only your error message.

您在 urls.py 中的url如下:

path('any_route_name/<int:course_pk>/', views.course_session, name='course_session')

Note:永遠不要忘記在你的urlroute_name的末尾傳遞/

然后,在您的任何template文件中,您可以訪問它並運行循環:

{% for study in study_courses %}
{{study.organizer}},{{study.date}}
{% endfor %}

然后,您可以訪問其所有屬性,並利用ManyToOne關系。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM