I am creating a web application using django, and I want now to add a view that modifies entries (my web application is an encyclopedia, so the view let us editing the page).
We may be able to access the editing page by cliking on the link of this html page:
{% extends "encyclopedia/layout.html" %}
{% block title %}
{{ title }}
{% endblock %}
{% block body %}
{{ html | safe }}
{% if exists %}
<br><br><br><br>
<a href="{{ address }}">Edit encyclopedia</a>
{% endif %}
{% endblock %}
So django'll go through this url
urlpatterns = [
...
...
...
path("<str:title>/edit", views.edit, name="edit"),
]
Then, this url should bring us to a this view:
def edit(request, title):
if request.method == "POST":
form = NewForm(request.POST)
if form.is_valid():
with open(f"entries/{title}.md", "w") as file:
file.write(form.cleaned_data["content"])
return redirect(reverse("encyclopedia:display"))
else:
return render(request, "encyclopedia/edit.html",{
'form' : NewForm(),
'message' : """<div class="alert alert-danger" role="alert">
Your entries are empty.
</div>"""
})
markup = util.get_entry(title)[0]
print(request.)
return render(request, "encyclopedia/edit.html",{
'form' : NewForm(),
'title' : title,
'markup': markup,
})
And here is my html file:
{% extends "encyclopedia/layout.html" %}
{% block title %}
Edit
{% endblock %}
{% block body %}
<h1>New Encyclopedia</h1>
<p>Our websites' encyclopedias are written in a langage names Markdow<br>
You may have additionnal informations about this language <a href="https://docs.github.com/en/free-pro-team@latest/github/writing-on-github/basic-writing-and-formatting-syntax">here</a>.</p>
<form action="{% url 'encyclopedia:edit' %}" method="POST" style="display: block; text-align: center; padding: 20px;">
{% csrf_token %}
<p style="margin: 15px;">{{ title }}</p>
<textarea rows='10' placeholder="Encyclopedia's content" name="content" style="width: 90%; margin: 15px;">{{ markup }}</textarea>
<input type="submit" value="Edit" style="width:15%">
</form>
{% endblock %}
But my problem is that when I run my application, and go to the editing page, I get a NoReverseMatch error just like this one:
NoReverseMatch at /wiki/Django/edit Reverse for 'edit' with no arguments not found. 1 pattern(s) tried: ['wiki/(?P[^/]+)/edit$']
I think that this problem is linked to the fact that I don't give the title argument when I call the editing view in my form, but I don't know how to do this.
If anyone could help me that would just be amazing, I made many researches, but couldn't really understand how to fix the problem...
I've finally found the origin of the error, to whom it may help, it was that I forgott to add the 'title' variable (that is obligatory for my view) just like this:
else:
return render(request, "encyclopedia/edit.html",{
'form' : NewForm(),
'message': """<div class="alert alert-danger" role="alert">
Your entries are empty.
</div>""",
'title' : title,
})
The is_valid()
method doesn't really corresponds to how do I want to treat data, I've replaced it with this function:
undecodables = ["è", "à", "é"]
def own_validation(string):
try:
valid = False
for i in string:
if i.isalnum : valid = True
assert i not in undecodables
except AssertionError:
return False
else:
return valid
I don't know what are all the undecodable characters, so the list'll be completed in the future.
Thanks to @iklinac for your help !