I am getting TypeError. Can someone help me,please?
HTML info:
{% csrf_token %}
<div>{{Content|safe}}</div>
<input type="submit" value="Edit">
URLS.py
path(“edit”,views.edit,name=“edit”),
Views
def edit(request,title):
if request.method == 'POST':
x = markdowner.convert(util.get_entry(title))
form = CreateForm(initial={'Content': x})
return render(request,"encyclopedia/edit.html",{"form":form,"title": title})
Hi,
Could you give some more information about the error you are getting, and the view you use?
At a minimum, please write out the full “TypeError line”, since it usually includes what function/method call failed, e. g. TypeError: convert() missing 1 required positional argument: 'text'
.
Other relevant questions to answer:
-
I’m assuming markdowner
is an instance of the MarkDown
class from the markdown2 package? It’s not a good thing that others need to guess this though, it’s far better if you explain your code and/or include import statements that make this clear.
-
What is util.get_entry
? Is it a self-written function? It’s probably best if you paste what you have in util.py
then (assuming util
is a module/script, rather than an object).
-
In your view’s code, the context you use makes use of a title
variable. What is that?
-
Your template seems to refer to a Content
variable, but there is no such key in the context dictionary ({"form":form,"title": title}
) used in your view’s code. Do you perhaps instead want to render your form, e. g. form.as_p
?
-
What is it that you want the template to do? Converting markdown into template tags and then feeding that into your form is unusual so the reason for doing so needs to be explained.
In order for others to be able to help, you need to explain what you seek to do, what you (your code) are actually doing right now, and in some detail what it is that goes wrong. It’s tricky to know exactly how to describe those things, or to know what’s relevant or not, but it’s a very valuable skill and crucial for getting help and/or collaborating with others.
TypeError- edit() missing 1 required positional argument: ‘title’
In above code,
markdowner
is an instance of the MarkDown
class from the markdown2 package.
util.get_entry(variable) is a function which returns contents based on the variable(‘title’) value.
Following are imports in View.py
from django.shortcuts import render
from . import util
from django.urls import reverse
from django.http import HttpResponseRedirect
from markdown2 import Markdown
from django import forms
from django.http import HttpResponse
from django.http import request
In the HTML template, when use click ‘Edit’ button, the page should allow the user to edit the content of the page. However, now the page itself is not displaying due to error.
OK, so for the error you’re getting, it’s apparent now that you wrote out the full TypeError
what is causing that error.
In your urls.py file you have path(“edit”,views.edit,name=“edit”),
. Your function definition reads def edit(request,title)
. The edit
function is expecting to be passed a title
value. You probably want to change your URL path so that it uses a path converter, e. g. path(“<str:title>/edit/”,views.edit,name=“edit”),
. You can read more about path converters in the Django docs.
Please use ``` backtick characters to enclose your code snippet blocks, or single ` backtick characters for inline code. It really helps readability.
Thank you so much!
You saved my days. I spent many hours to try to figure this mistake.
Thanks a lot,again!