C
C
Cyril2014-05-05 06:22:26
Django
Cyril, 2014-05-05 06:22:26

How to make an elementary button in django?

I'm trying to make a button on the "subscribe" page, I created a form according to the model, the button appeared, when I click in the PyCharm IDE logs, I get "POST /multiuniverse/1/ HTTP/1.1" 200 607" , but I don't understand how to process this response. the mymethod method was created for this, but how to register it in the button?

template:

<form action="#" method="post">
{% csrf_token %}
<input type="submit" class="btn" value="Button" name="mybtn">
</form>

views.py:
class  UniverseDetailView(DetailView):
    model = Members
    template_name = 'multiuniverse/universe_detail.html'

    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        context = self.get_context_data(object=self.object)
        return self.render_to_response(context)

    def mymethod(request):
        print ('Dont work :( ')    
        if(request.POST.get('mybtn')):
            print ('Works!')
        return render_to_response('App/yourtemplate.html')

models.py:
class Members(models.Model):            #ManyToMany
    uni = models.ForeignKey(Universe)
    profile = models.ForeignKey(Profile)
    ...

forms.py
class PodpiskaForm(ModelForm):
    class Meta:
        model = Members
        fields = ()

URLs.py:
from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView
from django.contrib import admin


admin.autodiscover()

urlpatterns = patterns('',

...

    url(r'^multiuniverse/', include('multiuniverse.urls')),

...
)

multiuniverse/urls.py:
from django.conf.urls import patterns, url

from views import UniverseListView, UniverseDetailView

urlpatterns = patterns('',

url(r'^$', UniverseListView.as_view(), name='multiuniverse'), 
url(r'^(?P<pk>\d+)/$', UniverseDetailView.as_view()), 
)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
rsludge, 2014-05-05
@rsludge

1. It's strange that you use DetailView for subscription, there are probably better candidates in standard Class Based Views, such as UpdateView.
2. In the form in the action field, you need to enter the url to which this form will be sent, now it is sent to the url of the current page.
3. The logic for changing the model through the form must be moved to the form class, in your case PodpiskaForm, by overriding one of the standard ModelForm methods

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question