1
I have the following urls:
# urls.py
url(r'^proposal/(?P<pk>\d+)/$', ProposalDetail.as_view(), name='proposal_detail'),
url(r'^contract/(?P<pk>\d+)/$', ContractDetail.as_view(), name='contract_detail'),
url(r'^proposal/edit/contract/$', 'create_contract', name='create_contract_url'),
It means that there is I have /proposal/1/
,
but in my example I will not have /contract/3/
still,
that is to say, /contract/3/
is a contract that does not yet exist,
and when there will be pk
which may be different from proposal
.
# views.py
class ProposalDetail(DetailView):
template_name = 'core/proposal/proposal_detail.html'
model = Proposal
To the contract
exist I will perform the following function...
def create_contract(request, pk):
if request.user.is_authenticated:
proposal = Proposal.objects.get(pk=pk)
if proposal.status != 'co':
return HttpResponse('O status do orçamento deve ser concluido.')
else:
contractor = proposal.work.customer
obj = Contract(
proposal=proposal,
contractor=contractor
)
obj.save()
proposal.status = 'a'
proposal.save()
return redirect('/contract/%d' % obj.id)
... from this button.
# proposal_detail.html
<form class="navbar-form navbar-right" action="." method="get">
<a class="btn btn-primary" href="{% url 'create_contract_url' proposal.id %}"><i class="icon-edit icon-white"></i> Criar Contrato</a>
</form>
The problem is I’m having trouble controlling pk
in the right way.
Question: How do I improve this code in order to inform the pk
of proposal
in function create_contract
and redirect the page to /contract/3
, whereas 3
will be a new pk
of contract
, but he doesn’t exist yet?
Yes, but I’m not using form, it’s just a button that I click and performs the function. But did you understand what I need about the id that doesn’t exist yet from the contract? I know I messed up the ids a bit, but I need to use the pk of Proposal just to reference me when creating the new contract. And then the redirect is to enter the new contract, when his pk exists.
– Regis Santos
I get it, but you can still use the urls the way I suggested. In both Updateview and Detailview, the Django default uses the "pk" to render the information of an already existing object. You can use the "proposal_id" nomenclature, in the future this will even help in code maintenance. In your view it would be def create_contract(request, proposal_id): .
– Puam Dias
Now I get it. Thank you.
– Regis Santos