我试图在基于类的视图CreateView中访问ForeignKeys。我希望能够从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 %}
内的尝试不起作用。)
非常感谢任何协助。
models.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)
views.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')
}
urls.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 %}
ingredient_form.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 %}
答案 0 :(得分:30)
您需要实例化您的食谱:
class IngredientAddView(CreateView):
model=Ingredient
def get_initial(self):
recipe = get_object_or_404(Recipe, slug=self.kwargs.get('slug'))
return {
'recipe':recipe,
}