How to achieve formView and ListView on one page [DJANGO 1.8] -
i trying create page following layout:
|---------------------------------------------------------------- | |----------------------------------| | | see details: owner (dropdown) | -> form | |----------------------------------| | | |----------------------------------| | | computer brand: dell | -> list of computers | | member_field1 | | | member_field2 | | | ------------------------------- | | | computer brand: asus v | -> first computer should have | | ------------------------------- | of details visible | | computer brand: tamagochi v | while next ones should | | | collapsed
my functionality when user selects owner dropdown redirected /owner/id/ , have details of computers associated him displayed bellow.
i have default "create new owner" field in dropdown first possible choice , see below create owner form.
i have been struggling hours trying understand of views, generic views , things.
things have (those not real examples code not compiled shows general things have in app right now):
# models class computer(models.model): brand = models.charfield(max_length=10) member_field1 member_field2 owner = models.foreignkey('owner') class owner(models.model): name = models.charfield(max_length=10) # forms class ownerform(forms.form): owner = forms.modelchoicefield(queryset=owner.objects.all()) # views class computerownerdetailview(generic.detailview): model = computer template_name = 'computer_owner_detail.html' def get_context_data(self, **kwargs): context = super.get_context_data context['owner_chooser_form'] = ownerform(initial = {'field1':context['object'].id}) context['owned_computers_list'] = computer.objects.all()[:10] return context # urls url(r'^owner/(?p<pk>[0-9]+)/$', views.computerownerdetailview.as_view(), name='owner_detail'), # template {% extends 'android/index.html' %} {% block where_it_has_to_go_in_base %} <form method="post" class="post-form">{% csrf_token %} {{ owner_chooser_form.as_p }} <button type="submit" class="save btn btn-default">save</button </form> {% if computer_owner_detail %} <ul> {% computer in computer_owner_detail %} <li>{{ computer.brand }}</li> {% endfor %} </ul> {% else %} <p> no computer defined yet </p> {% endif %} {% endblock %}
there several problems (questions) have this.
ownerform(initial = {'field1':context['object'].id}) => not preselect owner
context['owned_computers_list'] = computer.objects.all()[:10] => gets computers - want filter them owner have no access owner in method
i pretty sure whole architecture wrong. shouldn't using detailsview since not presenting details model have attached view ( computerownerdetailview -> computer ). not know how achieve required functionality.
right when submit form gets me same url post, can not handle post ( or @ least couldn't find way ) in current setup can see new id of owner user has selected dropdown , redirect owner/new_id/ computers associated new owner listed
can please me.
thank you
add following view:
from django.core.urlresolvers import reverse class computerownerdetailview(generic.formview): ... def form_valid(self,form): owner = get_object_or_404(owner,pk=form.cleaned_data['owner'] return httpresponseredirect(reverse('owner_details',owner.id))
if want custom page instead of 404:
from django.core.exceptions import objectdoesnotexist def form_valid(self,form): try: owner = owner(pk=form.cleaned_data['owner'] ulr = reverse('owner_details',owner.id) except objectdoesnotexist: url = reverse("another-view") return httpresponseredirect(url)
Comments
Post a Comment