Categories
Python Answers

How to add a form to a dynamically to a Django formset?

To add a form to a dynamically to a Django formset, we can use a for loop.

For instance, we write

<h3>My Services</h3>
{{ serviceFormset.management_form }}
{% for form in serviceFormset.forms %}
    <div class='table'>
    <table class='no_error'>
        {{ form.as_table }}
    </table>
    </div>
{% endfor %}
<input type="button" value="Add More" id="add_more">

to render the forms with form.as_table within the for loop.

We get the forms from serviceFormset.forms .

Categories
Python Answers

How to add a numeric for loop in python Django templates?

To add a numeric for loop in python Django templates, we can add it straight into the template.

For instance, we write

{% for i in '0123456789'|make_list %}
    {{ forloop.counter }}
{% endfor %}

to add a for loop in our template that loops from 0 to 9 since we have in '0123456789' as the loop expression.

We use the make_list filter to convert '0123456789' into a list.

Then we use forloop.counter to get the index.

Categories
Python Answers

How to look up a dictionary value with a variable with a Python Django template?

To look up a dictionary value with a variable with a Python Django template, we can add a custom template filter.

For instance, we write

from django.template.defaulttags import register

##...

@register.filter
def get_item(dictionary, key):
    return dictionary.get(key)

to use the register_filter decorator to add the get_item template filter.

In it, we call dictionary.get with the key to return the value of the given key.

Then we use it by using

{{ mydict|get_item:item.NAME }}

in our template

We use get_item with item.NAME we the key and mydict as the dictionary value.

Categories
JavaScript Answers

How to fix ‘CORS: Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true’ with Express and JavaScript?

To fix ‘CORS: Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true’ with Express and JavaScript, we can change the config of the cors middleware.

For instance, we write

const app = express();
const corsConfig = {
  credentials: true,
  origin: true,
};
app.use(cors(corsConfig));

to set the credentails and origin options to true when we add the cors middleware in our Express app to avoid this error.

Categories
Python Answers

How to upload a file in Python Django?

To upload a file in Python Django, we can get the upload files from request.FILES.

For instance, we write

for key, file in request.FILES.items():
    path = file.name
    dest = open(path, 'w')
    if file.multiple_chunks:
        for c in file.chunks():
            dest.write(c)
    else:
        dest.write(file.read())
    dest.close()

to loop through the request.FILES dictionary in our view function.

And then we can write the files into disk with write.

We can add a simple upload form with

<form action="/upload_file/" method="post" enctype="multipart/form-data">
  {% csrf_token %}
  <label for="file">Filename:</label>
  <input type="file" name="file" id="file" />
  <br />
  <input type="submit" name="submit" value="Submit" />
</form>

which has a file input.

The action attribute of the form is set to the URL of the file upload view.