0

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" class="form-control {% if form.errors %}invalid{% endif %}">
                 {{ 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 %}
5
  • What function? In what class? Code is VERY dirty ._. Commented Sep 12, 2022 at 4:54
  • @Som-1 code rewritten a million times. I know how to write correctly:D need form from class head in class stockpage. And everyone template and future classes as well. Commented Sep 12, 2022 at 5:20
  • You should just use a FormView, and you can use an {% include %}d fragment for the form on each page (or inherit it as a block from a super-template). Commented Sep 12, 2022 at 5:32
  • @AKX let me add templates Commented Sep 12, 2022 at 5:34
  • @AbdulAzizBarkat ok. Commented Sep 12, 2022 at 6:13

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

5 Comments

swears at "next" in context_processors.py and at url 'api:my_form' but there I don't know what I need to put. Help sansei! =)
what does it say for 'next'? As for 'api:my_form' you should reverse your url of the form view. E.g. in my urls it's like follows: path('my-form-view/', MyFormView.as_view(), name='my_form'), and the app name is api, then i reverse it as api:my_form.
also, next is a keyword in python. In case your editor swears on it, you may rename it to something like next_url or redirect_to, etc.
thx greatfully. I need to go, but I guess i will do that eventually.
I did it! That costs me a lot of nerves, but I closed this problem. Thank you. Found some sections where I need an urgent lessons =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.