Answer the question
In order to leave comments, you need to log in
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>
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')
class Members(models.Model): #ManyToMany
uni = models.ForeignKey(Universe)
profile = models.ForeignKey(Profile)
...
class PodpiskaForm(ModelForm):
class Meta:
model = Members
fields = ()
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')),
...
)
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. 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 questionAsk a Question
731 491 924 answers to any question