0

I am very new at Django and I am working on a web app project. this particular page is supposed to edit an entry and save the entry. but I keep getting the missing 1 required argument

Views.py

# editPage forms
class editform(forms.Form):
    content = forms.CharField(widget=forms.Textarea(), label='')


def edit(request, entry):
    if request.method == 'GET':
        page = util.get_entry(entry)
        return render(request,"encyclopedia/edit.html",{
            "form":SearchEntry(),
            "edit":editform(initial={'content': page}),
            "entry":entry
            })

    #If this is a POST request
    else:
        form = editform(request.POST)
        if form.is_valid():
            content = form.cleaned_data["content"]
            util.save_entry(entry,content)
            page = util.get_entry(entry)
            page = mark.convert(page)
            return render(request,"encyclopedia/entry.html",{
                "form":SearchEntry(),
                "page":page,
                "entry": title
            })

urls.py

from django.urls import path

from . import views

urlpatterns = [
    path("", views.index, name="index"),
    path("wiki/<str:entry>", views.entry, name="entry"),
    path("search", views.search, name="search"),
    path("newEntry", views.newEntry, name="newEntry"),
    path("edit", views.edit, name="edit"),

edit HTML

{% extends "encyclopedia/layout.html" %}


{% block title %}
    Edit {{name}}
{% endblock %}


{% block body %}
  <h1>{{title}}</h1>
  <form action= "{% url 'edit' %}" method="POST">
    {% csrf_token %}
    {{ edit }}
  <br>
  <input class="save btn btn-info" type="submit" value="save"/>
  </form>
  <p> Click the "save" button to save your entry to the encyclopedia.</p>
  <br>



<a href = "{% url 'index' %}"> Return Home</a>
{% endblock %}

entry HTML

{% extends "encyclopedia/layout.html" %}




{% block title %}
    Encyclopedia
{% endblock %}

{% block body %}

    <h1>{{title}}</h1>


    {{entry | safe}}

<a href = "{% url 'edit' %}"> Edit Content</a>
<br>
<br>
<a href = "{% url 'index' %}"> Return Home</a>
{% endblock %}

when I change this particular url:

path("edit/<str:entry>", views.edit, name="edit"),

I get a different issue: Reverse for 'edit' with no arguments not found. 1 pattern(s) tried: ['edit/(?P[^/]+)$']

  • Does this answer your question? [How to add url parameters to Django template url tag?](https://stackoverflow.com/questions/25345392/how-to-add-url-parameters-to-django-template-url-tag) – JPG Aug 25 '20 at 02:11
  • Since your edit view requires a *mandatory* parameter named `entry`, you should update the `url` tag usage. – JPG Aug 25 '20 at 02:13

1 Answers1

1

The problem is in your urls.py file in this line:

 path("edit", views.edit, name="edit"),

because views.edit is expecting that you must provide two-parameter request and entry in your url. And in your case entry is missing. Try to add entry in your urlspatterns path, in this case, I'm expecting your entry is int.

path("edit/<int:entry>", views.edit, name="edit"),

and this entry can be your model pk or anything else you want. After modifying your urlspatterns whenever you call edit view in your html you need to do this:

{% url 'edit' entry=your_entry_value %}

instead of:

{% url 'edit' %} 
Mubashar Javed
  • 1,197
  • 1
  • 9
  • 17
  • For more clear answer share your `utils.get_entry()` function. – Mubashar Javed Aug 25 '20 at 02:48
  • jade the util.get_entry() takes in strings: `def get_entry(title): """ Retrieves an encyclopedia entry by its title. If no such entry exists, the function returns None. """ ` try: f = default_storage.open(f"entries/{title}.md") return f.read().decode("utf-8") except FileNotFoundError: return None` – Anthony W Aug 25 '20 at 17:24
  • the util.get_entry() takes in strings: ```def get_entry(title): """ Retrieves an encyclopedia entry by its title. If no such entry exists, the function returns None. """ try: f = default_storage.open(f"entries/{title}.md") return f.read().decode("utf-8") except FileNotFoundError: return None``` – Anthony W Aug 25 '20 at 17:35