在Django中查看ForiegnKey数据

时间:2018-05-06 16:53:57

标签: python django django-models foreign-keys

我想创建一个显示Django中的外键数据的视图。

我有一些理发师可以在​​某些日期使用,我想知道哪些日期可以使用理发师。后来我需要知道哪些理发师在哪个日期可用。

models.py

from django.db import models
from datetime import time

class Barber(models.Model):

    BARBERS = (
        ('KK', 'Kim Kardashian'),
        ('CJ', 'Caitlin Jenner'),
        ('KW', 'Kanye West')
    )

    LOCATION = (
        ('CL', 'Central Detroit'),
        ('NL', 'North Detroit'),
        ('SL', 'South Detroit'),
        ('EL', 'East Detroit'),
        ('WL', 'West Detroit'),
    )

    name = models.CharField(max_length=2, choices=BARBERS)
    location = models.CharField(max_length=2, choices=LOCATION)

    def __str__(self):
        return self.name

class Date(models.Model):

    date = models.DateField()
    name = models.ForeignKey(Barber, on_delete=models.CASCADE)

我的应用中的urls.py

from django.urls import path
from .views import schedule, appointments

urlpatterns = [
    path('', schedule, name='schedule'),
    path('/appointments/<int:id>/', appointments, name='appointments')

views.py

from django.shortcuts import render, redirect
from .models import Barber, Date

def schedule(request):
    barber = Barber.objects.all()
    return render(request, 'schedule.html', {'barber': barber})

def appointments(request, name):
    dates = Date.objects.filter(name=name)
    return render(request, 'appointments.html', {'date': dates})

appointments.html

<h1>Appointments</h1>

<ul>
  {% for date in dates %}
    <li> {{ date.name }} {{ date.dates }} </li>
  {% endfor %}
</ul>

schedule.html:

<h1>Schedule</h1>

<ul>
  {% for barb in barber %}
    <a href="{% url 'appointments' Barber.id %}">
    <li> {{ barb.name }} {{ barb.location }} </li>
  {% endfor %}
</ul>

我得到的错误是:

Reverse for 'appointments' with arguments '('',)' not found. 1 pattern(s) tried: ['\\/appointments\\/(?P<id>[0-9]+)\\/$']

1 个答案:

答案 0 :(得分:2)

Maybe your code should be:

Option Explicit
Public Sub SSS()
    Dim lastRow As Long, i As Long
    Const WORD As String = "Hello" '<== Word you are looking for
    lastRow = 6

    With ThisWorkbook.Worksheets("Sheet1")
        For i = 2 To lastRow
            If Found((.Cells(i, 4).Value2), "\b(" + WORD + ")\b", False) Then .Cells(i, 2) = "a"
        Next i
    End With
End Sub
Public Function Found(ByVal t As String, ByVal inputPattern As String, Optional ignoreCaseOption = True) As Boolean
    Dim reg As Object
    Set reg = CreateObject("VBScript.RegExp")
    With reg
        .Global = True
        .MultiLine = True
        .ignoreCase = ignoreCaseOption
        .pattern = inputPattern
        If .test(t) Then Found = True
    End With
End Function

views

...
  <parameter>
    <parameterName value="?user_input" />
    <dbType value="object" />
    <size value="4000" />
    <layout type='log4net.Layout.SerializedLayout, log4net.Ext.Json'>
        <member value="user_input" />
    </layout>
  </parameter>
...