gpt4 book ai didi

django - 从 ForeignKey (non-self.request.user) 在 CreateView 中设置初始值

转载 作者:行者123 更新时间:2023-12-03 13:24:33 24 4
gpt4 key购买 nike

我正在尝试在基于类的 View CreateView 中访问 ForeignKey。我希望能够从 ForeignKeys 动态设置 CBV 中的初始值,并从 ForeignKeys 动态设置模板链接。

这两个问题(1.初始值,2.模板链接)可以用类似的方法解决,也可以用不同的方法解决……我还在学习中。也许第一个问题可以在 views.py 内解决。第二个问题可以用ingredient_form.html中的模板语法来解决?

我已经看到关于 SO 设置初始值的问题来自用户( self.request.user ),但不仅仅是 models.py 中的普通外键.

我正在经历django-by-errors ,并尝试添加额外的功能来扩展我的 django 知识。

我的问题特别集中在 views.py:IngredientAddView(CreateView) , 在ingredient_form.html , 和 urls.py:'recipe-detail' & 'ingredient-add '。

当我查看 'recipe-detail' ,我可以点击链接到'ingredient-add' .我要 'ingredient-add' “知道”哪个配方点击了它,并且能够将此配方设置为初始值(我在 views.py:IngredientAddView:get_initials(self) 中的尝试不起作用),并且还能够链接回这个配方(我在 ingredient_form.html:{% comment %} 中的尝试)不起作用)。

将不胜感激任何帮助。

模型.py

class Food(models.Model):
name=models.CharField(max_length=20,unique=True)

def __str__(self):
return self.name

def get_absolute_url(self):
return reverse('food-detail',kwargs={'pk':self.pk})

class Recipe(models.Model):
title=models.CharField(max_length=80,unique=True)
slug=models.SlugField(max_length=80,unique=True)
description=models.TextField(blank=True)

def __str__(self):
return self.title

def get_absolute_url(self):
return reverse('recipe-detail',kwargs={'slug':self.slug})

class Ingredient(models.Model):
recipe=models.ForeignKey(Recipe)
food=models.ForeignKey(Food)

def __str__(self):
return '%s (%s)' % (self.food, self.recipe)

View .py

class FoodListView(ListView):
model=Food

class FoodDetailView(DetailView):
model=Food

class FoodCreateView(CreateView):
model=Food

class RecipeListView(ListView):
model=Recipe

class RecipeDetailView(DetailView):
model=Recipe

class RecipeCreateView(CreateView):
model=Recipe

class RecipeUpdateView(UpdateView):
model=Recipe

class IngredientAddView(CreateView):
model=Ingredient

# def get_context_data(self,**kwargs):
# context=super(IngredientAddView,self).get_context_data(**kwargs)
# context['foreign']=self.request.session.get('slug')

def get_initials(self):
return {
'recipe':self.request.session.get('recipe')
}

网址.py

from .views import FoodListView, FoodDetailView, FoodCreateView, RecipeListView, RecipeDetailView, RecipeCreateView, RecipeUpdateView, IngredientAddView

urlpatterns=patterns('',
url(r'^$',RecipeListView.as_view(),name='recipe-list'),
url(r'^(?P<slug>[-\w]+)$',RecipeDetailView.as_view(),name='recipe-detail'),
url(r'^(?P<slug>[-\w]+)/edit$',RecipeUpdateView.as_view(),name='recipe-edit'),
url(r'^(?P<slug>[-\w]+)/add_ingredient/$',IngredientAddView.as_view(),name='ingredient-add'),
url(r'^new/$',RecipeCreateView.as_view(),name='recipe-create'),
url(r'^food/$',FoodListView.as_view(),name='food-list'),
url(r'^food/(?P<pk>[\d]+)$',FoodDetailView.as_view(),name='food-detail'),
url(r'^food/create/$',FoodCreateView.as_view(),name='food-create'),
)

recipe_detail.html

{% extends "base_food.html" %}

{% block title %}{{ recipe }} {% endblock %}

{% block content %}
<h1>{{ recipe }}</h1>
<p>{{ recipe.id }}</p>
<p>{{ recipe.title }}</p>

<br>
<h2>Description</h2>
<p>{{ recipe.description|default:'No description' }}</p>

<h2>Ingredients</h2>
<ul>
{% for ingredient in recipe.ingredient_set.all %}
<li>{{ ingredient }}</li>
{% endfor %}
</ul>
<p><a href="{% url 'ingredient-add' recipe.slug %}">Add ingredient</a></p>
<p><a href="{% url 'recipe-edit' recipe.slug %}">Edit recipe</a></p>
<p><a href="{% url 'recipe-list' %}">Back to recipe list</a></p>
{% endblock %}

成分表.html

{% extends "base_food.html" %}

{% block title %}Add Ingredient{% endblock %}

{% block content %}
<h1>Add Ingredient</h1>
<form method="POST">{% csrf_token %}
{{ form }}
<button type="submit" class="btn btn-primary">Save</button>
</form>

{%comment%} <p><a href="{% url 'recipe-detail' recipe.slug %}">Back to detail</a></p> {%endcomment%}
<p><a href="{% url 'recipe-list' %}">Back to recipe list</a></p>
{% endblock %}

最佳答案

您需要实例化您的配方:

class IngredientAddView(CreateView):
model=Ingredient

def get_initial(self):
recipe = get_object_or_404(Recipe, slug=self.kwargs.get('slug'))
return {
'recipe':recipe,
}

关于django - 从 ForeignKey (non-self.request.user) 在 CreateView 中设置初始值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18277444/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com