Send variable to Django crispy forms

Send variable to Django crispy forms

  

I have 2 HTML pages choose_type.html and post_form.html. On choose_type.html:

{% extends "blog/base.html" %}
{% load pagination_extras %}
{% block content %}
      <body>
            <div class="row">
                  <div class="col-sm-6 mb-3 mb-sm-0">
                    <div class="card">
                      <img src="/media/1.png" class="card-img-top m-auto" alt="..." style="width: 10rem;">
                      <div class="card-body">
                        <h5 class="card-title">Type 1</h5>
                        <button class="btn btn-primary" name="b1" value="1" id="btn" onclick="location.href='{% url 'post-create' %}'" >Report</button>
                      </div>
                    </div>
                  </div>
                  <div class="col-sm-6">
                    <div class="card">
                      <img src="/media/2.png" class="card-img-top m-auto" alt="..." style="width: 10rem;">
                      <div class="card-body">
                        <h5 class="card-title">Type 2</h5>
                        <button class="btn btn-primary" name="b1" value="2" id="btn" onclick="location.href='{% url 'post-create' %}'" >Report</button>
                      </div>
                    </div>
                  </div>
            </div>

I have 4 buttons (with descriptions, images, etc.) which have fixed value and all buttons have the same name. On click is URL to the same form defined by views.py:

@login_required
def PostCreate(request):
    if request.method == 'POST':
        form = PostFileForm(request.POST or None, request.FILES or None)
        files = request.FILES.getlist('file')
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            form.instance.author = request.user
        etc.
    else:
        form = PostFileForm()
    return render(request, 'post_form.html', {'form': form})

and post_form.html:

{% extends "blog/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
    <div class="content-section">
        <button class="btn btn-outline-info" id="audit" onclick="audit()">Basic</button>
        <form method="POST" id="PostForm" data-sektor-url="{% url 'ajax_load_sektors' %}" data-department-url="{% url 'ajax_load_departments' %}"  data-person-url="{% url 'ajax_load_persons' %}" novalidate enctype="multipart/form-data">
            {% csrf_token %}
            <fieldset class="form-group">
                <legend class="border-bottom mb-4" style="color:red;">Order</legend>
                {{ form|crispy }}
            </fieldset>
            <div class="form-group">
                <button class="btn btn-outline-info" id="submit" type="submit">Submit</button>
            </div>
        </form>

Depending on the 4 buttons which user can click to call the form, that form should have some fields hidden/shown or autofilled. I hide them using Javascript, for example:

<script type="text/javascript">
         function on_load(){
            document.getElementById('hidden_on_open').style.display = 'none';

My problem is, I don't know which button is pressed. Is there a way to 'pass a static variable' or something to the post_form.html from choose_type.html, so that I can use Javascript to hide/show and autofill certain fields depending on that variable. Just simple 0/1/2/3 would do the trick. I'm trying to use a same form for multype uses. I don't need models, I just need it for 1 time for form styling.

url.py:

urlpatterns = [
    path('choose_type/', views.choose_type, name='blog-choose_type'),
    path('info/nova/', views.PostCreate, name='post-create'),
]

So far I have tried:

To get data from (as suggested below) choose_type.html I have edited views.py to get 'b1' value, but value is always NONE:

@login_required
def PostCreate(request):
    x = request.POST.get('b1')
    if request.method == 'POST':
        form = PostFileForm(request.POST or None, request.FILES or None)
        files = request.FILES.getlist('file')
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            etc.
    else:
        form = PostFileForm()
    return render(request, 'post_form.html', {'form': form})

Can I add a fix variable to link, e.g. def PostCreate(request, x): and have each button have different url onclick="location.href='{% url 'post-create' 2%}'"? But I don't know how to implement this.

Answer

Try passing a fixed variable through the URL and then using it in views and template.

like this.

path('info/nova/<int:type_id>/', views.PostCreate, name='post-create')

Then accept it in views.py and pass as a context dictionary to template.

@login_required
def PostCreate(request, type_id):
    if request.method == 'POST':
        form = PostFileForm(request.POST or None, request.FILES or None)
        files = request.FILES.getlist('file')
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            form.instance.author = request.user
    else:
        form = PostFileForm()

    return render(request, 'post_form.html', {'form': form, 'type_id': 
    type_id})

Now accept that variable on frontend and use javascript for hiding and showing it.

I hope this will help.

© 2024 Dagalaxy. All rights reserved.