Sup, need to use a function in a different class without losing the model, while still being able to change the function(not important, I can rewrite func into more flexible func). Can't solve this issue at least 10 hours. Google didnt help =( I know code is a little dirty, I will clean that :D
Don't worry
I have same header with form and another form in all pages.
Need form from class head in class stockpage. And everyone template and future classes as well.
Views
class head(View):
template_name = "dental/mainpage.html"
model = signup
def get(self,request):
if request.method == 'GET':
form = UserSign
return render(request, "dental/mainpage.html", {"form": form})
def FormSign(self,request):
if request.method == 'POST':
form = UserSign
if form.is_valid():
body = {
'Name': form.cleaned_data['Name'],
'email': form.cleaned_data['email'],
'Number': form.cleaned_data['Number'],
}
info_user = "\n".join(body.values())
send_mail(
'Sign',
info_user,
'[email protected]',
['[email protected]'],
fail_silently=False)
return render(request, "dental/mainpage.html", {"form": form}) #self.template_name
return render(request, "dental/mainpage.html", {"form": form})
class stockpage(ListView):
template_name = "dental/stocks.html"
model = stock
context_object_name = "stocks"
HTML
signuphtml
{% load static %}
{%block css_files %}
<link rel="stylesheet" href="{% static "signup.css" %}">
{% endblock %}
{% block content %}
<section id="forms">
<form action="" method="post">
{% csrf_token %}
<div id="txt" >
{{ form.label_tag }}
{{ form }}
{{ form.errors }}
</div>
<div id="buttsig">
<button id="b4" type="submit">записаться</button>
</div>
</form>
</section>
{% endblock content %}
mainpage.html
{% extends 'main.html' %}
{% load static %}
{%block css_files %}
<link rel="stylesheet" href="{% static "header.css" %}">
<link rel="stylesheet" href="{% static "mainpage.css" %}">
<link rel="stylesheet" href="{% static "signup.css" %}">
<link rel="stylesheet" href="{% static 'assets/css/bootstrap.css' %}">
{% endblock %}
{% block title %}{% endblock %}
{% block content %}
{% include "dental/includes/header.html" %}
<div ="d1">
<ul>
<li>blabla</li>
<li>blabla</li>
<li>blabla</li>
<li>blabla</li>
</ul>
</div>
{% include "dental/includes/signup.html" %}
{% include "dental/includes/tail.html" %}
{% endblock content %}
stocks html
{% extends 'main.html' %}
{% load static %}
{%block css_files %}
<link rel="stylesheet" href="{% static "header.css" %}">
<link rel="stylesheet" href="{% static "mainpage.css" %}">
<link rel="stylesheet" href="{% static "signup.css" %}">
<link rel="stylesheet" href="{% static 'assets/css/bootstrap.css' %}">
<link rel="stylesheet" href="{% static "stocks.css" %}">
{% endblock %}
{% block title %}{% endblock %}
{% block content %}
{% include "dental/includes/header.html" %}
<section id="contentstock">
{% for stock in stocks %}
<li>
<h4>{{ stock.title }}</h4>
<img src="{{ stock.image.url }}" alt="{{ stock.image }}"/>
<h4>{{ stock.content|linebreaks}}</p>
</li>
{% endfor %}
</section>
{% include "dental/includes/signup.html" %}
{% include "dental/includes/tail.html" %}
{% endblock content %}
CodePudding user response:
Try adding separate view for handling the form with hidden field for url to redirect to. Django's default auth views work like this. Also custom context processor may help to provide the form for all pages without overriding all their get
or get_context_data
methods.
Consider the example:
forms.py:
class MyForm(forms.Form):
next_url = forms.CharField(widget=forms.HiddenInput, required=False)
name = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput)
context_processors.py:
def my_form_context_processor(request):
my_form = MyForm()
my_form.fields['next_url'].initial = request.get_full_path()
return {'my_form': my_form}
settings.py:
TEMPLATES = [
{
...
'OPTIONS': {
'context_processors': [
...
'my_app.context_processors.my_form_context_processor',
],
},
},
]
views.py:
class MyFormView(FormView):
form_class = MyForm
template_name = 'my_form.html'
def form_valid(self, form):
# ...
next_url = form.cleaned_data.get('next_url')
if not next_url:
next_url = reverse('my_app:home') # default
return HttpResponseRedirect(next_url)
class ViewA(TemplateView):
template_name = 'template_a.html'
class ViewB(TemplateView):
template_name = 'template_b.html'
template_a.html:
{% include "my_form.html" %}
<h1>A</h1>
template_b.html:
{% include "my_form.html" %}
<h1>B</h1>
my_form.html:
<form action="{% url 'my_app:my_form' %}" method="POST">
{% csrf_token %}
{{ my_form.as_p }}
<p><input type="submit" value="Go!"></p>
</form>