i have some class
class Aid(models.Model):
class Meta:
verbose_name = 'test'
id = models.AutoField(primary_key=True)
organization_name = models.CharField(max_length=150, null=False, blank=False)
def __str__(self):
return self.organization_name
i am add base64
def set_fields(self):
fields_to_encode = [
self.organization_name,
]
encoded_fields = []
for field in fields_to_encode:
field_bytes = field.encode('utf-8')
encoded_field = base64.b64encode(field_bytes).decode('utf-8')
encoded_fields.append(encoded_field)
return encoded_fields
def get_fields(self, encoded_fields):
decoded_fields = []
for encoded_field in encoded_fields:
if encoded_field:
field_bytes = base64.b64decode(encoded_field.encode('utf-8'))
decoded_field = field_bytes.decode('utf-8')
decoded_fields.append(decoded_field)
else:
decoded_fields.append(None)
return decoded_fields
def save(self, *args, **kwargs):
encoded_fields = self.set_fields()
self.organization_name = encoded_fields[0]
super().save(*args, **kwargs)
Now when run post method have error
Access error (403)
CSRF check failed. Request rejected.
Please help for resolve this issue.
Side note: When posting code here, enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted. (I’ve taken the liberty of fixing your original post for this. Please remember to do this in the future.
This error has nothing to do with your model or the view being access by your POST.
See How to use Django’s CSRF protection | Django documentation | Django and Cross Site Request Forgery protection | Django documentation | Django for more details.
If you need more specific assistance here, please post the view and the template creating the page that is issuing the POST request throwing the error.
2 Likes
onyeibo
December 23, 2023, 8:02am
3
Post the template that is firing that page, please
{% extends 'base/base.html' %}
{% block content %}
<section id="humanitarian-aid-form" class="container my-5">
<div class="row justify-content-center">
<div class="col-12 col-md-10 text-center">
<p>
test
</p>
</div>
</div>
<div class="text-danger">
* Required fields
</div>
<form enctype = "multipart/form-data" method="POST">
{% csrf_token %}
<input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">
<fieldset>
{% for field in form %}
<div class="form-group">
{% if field.name == 'agreement' %}
{{ field }}
<label for="{{ field.name }}" class="form-label mt-4">{{ field.label }}*</label>
{% else %}
{% if field.field.required %}
<label for="{{ field.name }}" class="form-label mt-4">{{ field.label }}*</label>
{% else %}
<label for="{{ field.name }}" class="form-label mt-4">{{ field.label }}</label>
{% endif %}
{{ field }}
{% endif %}
{% if field.errors %}
<div class="alert alert-danger">
{{ field.errors }}
</div>
{% endif %}
{% if field.help_text %}
<small id="emailHelp" class="form-text text-muted">{{field.help_text}}</small>
{% endif %}
</div>
{% endfor %}
</fieldset>
<input type="submit" value="SEND REQUEST" class="btn btn-danger">
</form>
</section>
{% endblock %}
You don’t supply the input
tag for the token - that is what the csrf_token
tag does for you. Remove that second line from your template.
1 Like