Issue
One of my view needs to add an item, along with other functionality, but I already have another view which specifically adds an item.
Can I do something like:
def specific_add_item_view(request):
item = Item.objects.create(foo=request.bar)
def big_view(request):
# ...
specific_add_item_view(request)
Solution
View functions should return a rendered HTML back to the browser (in an HttpResponse
). Calling a view within a view means that you're (potentially) doing the rendering twice. Instead, just factor out the "add" into another function that's not a view, and have both views call it.
def add_stuff(bar):
item = Item.objects.create(foo=bar)
return item
def specific_add_item_view(request):
item = add_stuff(bar)
...
def big_view(request):
item = add_stuff(bar)
...
Answered By - Seth
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.