我正在遵循本教程https://tutorial.djangogirls.org/en/extend_your_application/,但是在尝试使用path方法将pk从html传递到url时收到Templatesyntax错误。
根据我对这个错误的了解,这与花括号和引号有关,但是在这种情况下,我无法找出语法的确切问题。
这是listview.html
{% for vehicle_list_load in vehicle_list_loads %}
<tr>
<td>{{vehicle_list_load.vehicle_num}}</td>
<td>{{vehicle_list_load.Driver_name}}</td>
<td>{{vehicle_list_load.BusinessUnit}}</td>
<td>{{vehicle_list_load.CheckinTime}}</td>
<td>{{vehicle_list_load.Type}}</td>
<td>
<a href= "{% url 'vehicle_movement:checkoutview' pk = vehicle_list_load.pk %}" class = "glyphicon glyphicon-pencil" aria-hidden ="true" > Edit</a>
</td>
</tr>
{% endfor %}
这是vehicle_movements urls.py
from django.urls import path
from vehicle_movement import views
app_name = 'vehicle_movement'
urlpatterns = [
path('checkoutview/<int:pk>/',views.checkout, name = 'checkoutview'),
]
这是主要的urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include(('vehicle_movement.urls','vehicle_movement'),namespace = 'vehicle_movement')),
]
这是视图
def listView(request):
vehicle_list_loads = list(Checkin.objects.all().filter(Type ='Loading'))
vehicle_list_unloads = list(Checkin.objects.all().filter(Type ='Unloading'))
current_time = datetime.utcnow().replace(tzinfo=utc)
diff = current_time
return render(request,'vehicle_movement/listView.html',
{'vehicle_list_loads':vehicle_list_loads,'vehicle_list_unloads':vehicle_list_unloads,'diff':diff})
点击“编辑”,此视图需要打开
def checkout(request,pk):
post = get_object_or_404(Checkin, pk= pk)
return render(request,'vehicle_movement/checkout.html',{'post':post})
答案 0 :(得分:0)
您的urls.py似乎设置正确,我认为它不适用于您的原因是因为参数周围有多余的空格。在模板中传递参数的正确方法是这样的:
{% url 'vehicle_movement:checkoutview' pk=vehicle_list_load.pk %}