I feel kinda bad using my first post as a cry for help but hey, im certainly not the first lulz, anyway, im teaching myself python/django and im really stuck atm and ive been slogging through problems myself lately and wasting a lot of time doing it and this one has me stuck.
Im getting the error: NoReverseMatch at /messages/newreply/1/
Reverse for 'newreply' with arguments '('',)' not found. 1 pattern(s) tried: ['messages/newreply/(?P<post_id>[0-9]+)/\Z']
This is my url file;
app_name = 'board'
urlpatterns = [
path('', views.index, name='index'),
path('<int:post_id>/', views.postdetail, name='detail'),
path('newmsg/', views.newmsg, name='newmsg'),
path('newreply/<int:post_id>/', views.newreply, name='newreply')
view
def newreply(request, post_id):
post = Post.objects.get(id=post_id)
if request.method != "POST":
form = ReplyForm()
else:
form = ReplyForm(data=request.POST)
if form.is_valid():
newreply= form.save(commit=False)
newreply.post = post
newreply.save()
return redirect('board:index')
context = {'form':form}
return render(request, 'board/newreply.html', context)
template;
{% extends 'pages/base.html' %}
{% block content %}
<p>Add your reply here</p>
<form action="{% url 'board:newreply' post.id %}"method ='post'>
{% csrf_token %}
{{ form.as_p }}
<button name = "submit">Add post</button>
</form>
{% endblock content %}
Ive tried so many things now im not even sure where i began anymore so id really appreciate any help, especially knowing why its actually happening as ive read through a few posts with the same type of error but trying the solutions posted has produced other errors for me.
Thank you!
It is indeed a useless and annoying error. Usually, it means you haven't passed something in the context of the request. In this case, the post object.
context = {'form':form, 'post': post}
return render(request, 'board/newreply.html', context)
Related
I have a problem with my login template. I´m not sure, but I think I´m using a custom form since the path is templates/registration/login.html . So, I think it gets replaced to the usual django form. But the input fields of my form dont show up, so I only have the login button and some text underneath it. Similar questions I read couldn´t solve it, so I hope someone can me help here. If you need any code more, please let me know. Thanks in advance.
views.py
def login(response):
if response.method == "POST":
form = LoginForm(response.POST)
if form.is_valid():
username = form.cleaned_data.get("username")
password = form.cleaned_data.get("password")
user = authenticate(username=username, password=password)
auth_login(response,user)
return redirect("home")
else:
form = LoginForm()
return render(response, "registration/login.html",{})
urls.py
from register import views as v
path("", v.login, name="login"),
login.html
{% block content %}
<form method="POST">
{% csrf_token %}
<div class="content">
<h3>{{form.username}}</h3>
<h3>{{form.password}}</h3>
<button type="submit">Login</button>
</div>
</form>
<br>
<p>Don´t have an account yet?</p>
<p>Create one here</p>
{% endblock %}
The render() function takes the request object as its first argument, a template name as its second argument and a dictionary as its optional third argument.
so, you should pass {"form": form} as the third argument to the render function.
After the FamilyForm is Filled and redirect the PersonForm is not get post. they are two forms to fill. adding action to the form also didn't trigger it. there might be something small that has been missed in it. please help as soon as possible
for any other information tag and let me know
View.py
def index(request):
familyForm = FamilyForm()
if request.method == 'POST':
familyForm = FamilyForm(request.POST)
if familyForm.is_valid():
familydetails = familyForm.save()
return redirect(addPerson,familydetails.familyId)
#return render(request, 'html/personForm.html',{"family":familydetails,'personForm':personForm})
return render(request, 'html/index.html', {"familyForm": familyForm})
def addPerson(request,FamilyID):
family = FamilyDetails.objects.get(familyId=FamilyID)
personForm = PersonForm()
print(request.method)
if request.method == 'POST':
personForm = PersonForm(request.POST)
print(personForm.errors)
if personForm.is_valid():
personDetails = personForm.save(commit=False)
print(personDetails)
return HttpResponse("<h1>Form FIlled</h1>")
return render(request,'html/personForm.html', {"personForm":personForm, 'family':family })
personForm.html
{% extends 'html/familyCard.html' %}
<div class="formRepeat">
<form method="post" action="{% url 'add_person' family.familyId %}" id="personForm" class="needs-validation" novalidate
data-qualification-url="{% url 'ajax_load_qualification' %}">
{% csrf_token %}
urls.py
urlpatterns = [
path('', views.index, name="index"),
path('addperson/<int:FamilyID>/', views.addPerson, name="add_person"),
path('ajax/load-qualification/',views.load_qualification,name='ajax_load_qualification'),
]
i think that in redirecting the user to (add_person) page you made mistake
return redirect("add_person", familydetails.familyId) but you wrote function name instead url name
Try this in your html
{% url 'add_person' FamilyID=family.familyId %}
or
{% url 'add_person' FamilyID.family.familyId %}
maybe one of this can help you
I'm receiving this error:
NoReverseMatch at /comments_page/1/post_comment/
Reverse for 'post_comment' with arguments '('',)' not found. 1 pattern(s) tried: ['comments_page/(?P[0-9]+)/post_comment/$']
My views.py
def post_comment(request, product_id):
host_product = Product.objects.get(pk=product_id)
comment = Comment()
comment.product = host_product
comment.author = request.POST["author"]
comment.comment_text = request.POST["comment"]
comment.save()
return render(request, 'comments_page/detail.html', {"host_product": host_product})
My comments_page\urls.py
from django.conf.urls import url
from . import views
app_name = "comments_page"
urlpatterns = [
# /comments_page/
url(r'^$', views.index, name="index"),
# /comments_page/1/
url(r'^(?P<product_id>[0-9]+)/$', views.detail, name="detail"),
# /comments_page/1/post_comment/
url(r'^(?P<product_id>[0-9]+)/post_comment/$', views.post_comment, name='post_comment'),]
My detail.html
<form action="{% url 'comments_page:post_comment' product.id %}" method="post">
{% csrf_token %}
Name: <input type="text" id="author" name="author">
Comment:
<textarea id="comment" name="comment"></textarea><br>
<input type="submit" value="Post Comment">
I think I've identified the problem as being in the product.id here
{% url 'comments_page:post_comment' product.id %}
in the html page. I've tried formatting this a couple of different ways, but I haven't had any luck. Do note, the comment is going through and the form and it works as far as updating the database and loading the entry on the page goes, but the page is not being redirected. I have to reload it manually. Any help would be appreciated.
The error message shows that the argument you pass to the {% url %} tag does not exist and resolves to an empty string. Your view indeed does not pass in a product variable, only a host_product variable. You need to change the tag accordingly:
{% url 'comments_page:post_comment' host_product.id %}
For those who may wonder, the fix is to change the return function in views.py to this
return render(request, 'comments_page/detail.html', {"product": host_product})
I do not understand why this works, but it does. Any suggestions as to how to clean up my post_comment function would be appreciated. I feel it's overly convoluted by using host_product
I've been trying to figure this out for a while now but I feel like I don't know the framework well enough to debug this myself.
Basically I'm creating a little blog style site and I'm trying to create a list of posts which can link to the page to read the post itself.
I have a for loop in my template:
templates/home.py
<h1>Home Page</h1>
<p>welcome to the ven home page, {{ username }}!</p>
Click here to log out
<br>
Click here to create a post
<h2>Posts:</h2>
{% for post in posts %}
<div>
<hr>
<h4>{{post.title}}</h4>
<p>{{post.body}}</p>
<p><i>{{post.tags}}</i></p>
</div>
{% endfor%}
It's the line <h4>{{post.title}}</h4> which is causing the problem. I'm getting the error
Reverse for 'show' with keyword arguments '{'id': 1}' not found. 1
pattern(s) tried: ['posts/(?P<post_id>\\d+)/view/$']
here is my urls file
url(r'^$', views.CreateFormView.as_view(), name='create'),
url(r'^(?P<post_id>\d+)/view/$', views.show_post, name='show')
The create method link works fine
and here is the view which loads the template:
def home(request):
if not request.user.is_authenticated:
return redirect('users:login')
posts = Post.objects.all()
username = request.user.username
return render(request, 'ven/home.html', {'username': username, 'posts':
posts})
If more information is needed then let me know and I will provide it.
All other answers have said that this error is to do with the namespace, but it's working fine with the create link so I'm stumped.
Thanks in advance!
The argument names are mismatching.
You'd want to change <h4>{{post.title}}</h4>
to
<h4>{{post.title}}</h4>
Since in urls.py the show url is defined as '^(?P<post_id>\d+)/view/$'.
I feel like I'm tripping. I am trying to add a url with-dashes. I'm not sure what I'm doing wrong using non-classbased views. I don't think I can render a definition like blog_post.as_view() since it doesn't have it.
Does anyone see an obvious error?
Error Message:
Reverse for 'blog_post' with arguments '(u'i-prefer-debian',)'
and keyword arguments '{}' not found. 1 pattern(s) tried:
['blog/$post/(?P<slug>[\\w-]+)/$']
urls.py
url(r'^post/(?P<slug>[\w-]+)/$', 'blog_post', name="blog_post"),
views.py
def blog_post(request, slug):
print 1 # To see if it gets hit
context = {
'post': get_object_or_404(Posts, slug=slug)
}
return render(request, 'blog_post.html', context)
blog_list.html
{% for post in posts %}
<div>
{{ post.title }}
{{ post.created_at }}
</div>
{% endfor %}
The problems comes from the urls.py file where you include the urls.py you are showing.
It looks like you did:
url(r'^blog/$', include('blog.urls'))
You need to drop the $ (ref).