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).
Related
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)
I'm getting this error and I couldn't find why? Please help me with this error. Below is the code from my project
urls.py
path("fees/<int:service_id>", views.service_rates_view, name='service_fee')
index.html
{% for service in main_services %}
{{ service.name }}
{% endfor %}
view.py
def service_fee_view(request, service_id):
service = Service.objects.get(pk=service_id)
subservices = service.subservice_set.all()
return render(request, 'emitr/service.html', {
'service': service,
'subservices': subservices
})
Did you checked the name of the fbv is matching? In urls.py, you wrote it service_rates_view in views.service_rates_view, but service_fee_view in the view.py
It seems like your service.id is empty.
Since Django models require a primary key, you could use that instead
views.py
...
path("fees/<service_id>/", views.service_rates_view, name='service_fee')
...
index.html
{% for service in main_services %}
{{ service.name }}
{% endfor %}
You can also look into ListView and DetailView if they may be of help.
I got an error,
TypeError at /app/^detail/(?P1[0-9]+)/$
detail() got an unexpected keyword argument 'pk'
.
I wrote urls.py
urlpatterns = [
path('top/', views.top, name='top'),
path(r'^detail/(?P<pk>[0-9]+)/$',views.detail , name='detail'),
]
in views.py
def top(request):
content = POST.objects.order_by('-created_at')
page = _get_page(blog_content, request.GET.get('page'))
return render(request, 'top.html',{'content':content,"page":page})
def detail(request):
content = POST.objects.order_by('-created_at')
return render(request, 'detail.html',{'content':content})
in top.html
<div>
{% for content in page %}
<div>
<h2>{{ content.title }}</h2>
</div>
{% endfor %}
</div>
<div>
{% for content in page %}
<h2>{{ content.title }}</h2>
<p>SHOW DETAIL</p>
{% endfor %}
</div>
When I put "SHOW DETAIL" button, this error happens.I really cannot understand why I can't access pk. pk is default value, so I think I can access it from everywhere.I wanna make a system when I put "SHOW DETAIL button",content's detail is shown.What is wrong in my code?How should I fix this?Am I wrong to write url?Or is this error's meaning I should write pk in detail method?
You're using the new path() function, which does not take a regex. Either go back to the old url() function, or use <type:name> syntax:
path('detail/<int:pk>/', ...)
In the view function detail(), there's no argument named pk. Add it as the second argument should solve your problem:
def detail(request, pk):
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
quite new to Django and Python. Hoping to get some help on what went wrong with the below code. Basically, I am trying to create a url with and id and a slugfield e.g. something.com/1/arts-and-crafts.
Stucked and getting a NoReverseMatch error below:
Reverse for 'category' with arguments '(2, u'arts-and-crafts')' and keyword arguments '{}' not found. 2 pattern(s) tried: ['(?P\d+)/[-\w]+/$', '(?P\d+)/$']
Below is the code that I am using, would greatly appreciate any help in understanding what went wrong:
template html
<ul class="list-unstyled">
{% for category in categories %}
{% if forloop.counter < 10 %}
<li><a href="{% url 'category' category.id category.slug %}">
{{ category.cat_name}}</a></li>
{% endif %}
{% endfor %}
</ul>
url.py
url(r'^(?P<cat_id>\d+)/[-\w]+/$', 'guides.views.category', name='category'),
views.py
def category(request, cat_id):
categories = Category.objects.all()
guides = Guide.objects.filter(category__id=cat_id).order_by("created_date").reverse()
is_featured = Guide.objects.filter(category__id=cat_id).filter(featured=True)
selected_category = get_object_or_404(Category, pk=cat_id)
return render(request, 'guides/category.html', {'categories': categories, 'guides': guides, 'is_featured': is_featured, 'selected_category': selected_category})
Your URL doesn't capture the slug as a parameter, so it can't be used in the reverse call. Either change the second pattern to (?P<slug>[-\w]+) or don't use category.id in the {% url %} tag.