Getting NoReverseMatch even though url patterns are correct? - python

I've been learning Django for like a month so apologies if I'm doing something stupid, but, I'm getting a NoReverseMatch Reverse for 'profile' with arguments '('',)' not found. 1 pattern(s) tried: ['profile/(?P<use>[^/]+)$'] error when trying to render a "profile" page under the url '/profile/[a user's name]', and I'm pretty sure all my paths/patterns are correct?? Here's my code, could someone help me with this? All my other url patterns work fine. This path also worked fine before, I just renamed the "user" variable into "use" and this happened.
urls.py:
urlpatterns = [
path("", views.index, name="index"),
path("login", views.login_view, name="login"),
path("logout", views.logout_view, name="logout"),
path("register", views.register, name="register"),
path("profile/<str:use>", views.profile, name="profile"),
path("following", views.following, name="following"),
#api
path('', include(router.urls)),
]
views.py:
def profile(request, use):
currentuser = str(request.user)
followers = []
following = []
for follow in Follow.objects.filter(following=use):
followers.append(follow.person)
for item in Follow.objects.filter(person=use):
following.append(item.following)
follower_count = len(followers)
following_count = len(following)
try:
Follow.objects.get(person=currentuser, following=use)
followed = True
except:
followed = False
post_list = Post.objects.filter(user=use)
dates = []
for post in post_list:
dates.append(post.timestamp)
dates.sort(key = lambda date: datetime.strptime(date, '%B %d, %Y, %I:%M %p'), reverse=True)
formattedlist = []
for date in dates:
postobject = Post.objects.get(timestamp=date)
formattedlist.append(postobject)
paginatorlist = Paginator(formattedlist, 10)
now_page_content = paginatorlist.page(1)
nowpage = 1
if request.method == "POST":
# Update the database
followbutton = request.POST.get("button")
button = request.POST.get("nav-button")
pagenum = request.POST.get("pg")
if followbutton:
if followbutton == "Follow":
followid = Follow.objects.all().count() + 1
person = currentuser
following = use
newfollow = Follow(followid, person, following)
newfollow.save()
return HttpResponseRedirect(f"/profile/{use}")
else:
deletefollow = Follow.objects.get(person=currentuser, following=use)
deletefollow.delete()
return HttpResponseRedirect(f"/profile/{use}")
else:
currentpage = paginatorlist.page(pagenum)
if button == 'Previous':
if pagenum == '1':
return render(request, "network/profile.html", {
"formattedlist": now_page_content, "currentnumber": nowpage, "message":'No previous page', "use": use, "currentuser": currentuser, "follower_count": follower_count, "following_count": following_count, "followed": followed,
})
else:
nowpage = int(pagenum)-1
now_page_content = paginatorlist.page(nowpage)
return render(request, "network/profile.html", {
"formattedlist": now_page_content, "currentnumber": nowpage, "use": use, "currentuser": currentuser, "follower_count": follower_count, "following_count": following_count, "followed": followed,
})
else:
if currentpage.has_next() == False:
return render(request, "network/profile.html", {
"formattedlist": currentpage, "currentnumber": pagenum, "message":'No next page', "use": use, "currentuser": currentuser, "follower_count": follower_count, "following_count": following_count, "list": formattedlist, "followed": followed,
})
else:
nowpage = int(pagenum)+1
now_page_content = paginatorlist.page(nowpage)
return render(request, "network/profile.html", {
"formattedlist": now_page_content, "currentnumber": nowpage, "use": use, "currentuser": currentuser, "follower_count": follower_count, "following_count": following_count, "followed": followed,
})
return render(request, "network/profile.html", {
"use": use, "currentuser": currentuser, "follower_count": follower_count, "following_count": following_count, "followed": followed, "formattedlist": now_page_content, "currentnumber": nowpage,
})
network/profile.html
{% extends "network/layout.html" %}
{% block body %}
<script src="http://127.0.0.1:8000/static/network/network.js"></script>
<div>
<h2 class="profiletext">{{use}}'s profile page</h2>
{{use}}
{% if message %}
<h6 style="color: red; margin-left: 10px">{{message}}</h6>
{% endif %}
<h5 class="profiletext">Follows: {{follower_count}} // Following: {{following_count}}</h5>
{% if use != currentuser %}
<form method="POST" class="profiletext" action="{% url 'profile' use %}">
{% csrf_token %}
{% if followed == False %}
<input type="submit" value="Follow" name="button">
{% else %}
<input type="submit" value="Unfollow" name="button">
{% endif %}
</form>
{% endif %}
</div>
{% for post in formattedlist %}
<div class="post">
<h5>{{post.use}}</h5>
<h6 style="font-weight: 60" class="edit"><a>Edit</a></h6>
<h4 class="content" style="font-size: 16px;">{{post.content}}</h4>
<h6 style="color: #d3d3d3; font-weight: 50">{{post.timestamp}}</h6>
<h1 class="hidden">{{post.id}}</h1>
{% if user.is_authenticated %}
<img src="https://cdn.shopify.com/s/files/1/0649/0603/products/bigo-0002-GH-8-bit-heart_grande.jpeg?v=1410449344">
<h2 class="likes">TODO</h2>
{% endif %}
</div>
{% endfor %}
<div>
<form class="pagination" method="POST" action="{% url 'profile' use %}">
{% csrf_token %}
<input type="submit" value="Previous" class="page-link" name="nav-button">
<input type="submit" value="Next" class="page-link" name="nav-button" >
<input type="text" value={{currentnumber}} class="pagenumber" name="pg">
</form>
</div>
{% endblock %}

I assume you have not changed user foreignkey from Post model. So, I guess your error is from this line:
<h5>{{post.use}}</h5>
So, it should be like:
<h5>{{ post.user }}</h5>

Related

"POST /......... / ......... / HTTP/1.1" 405 0 - Django

I have a problem, in my work I need to insert a system of likes to published posts but by clicking on the button that should give the post a like, nothing happens and this error comes out..."POST /posts/like/ HTTP/ 1.1" 405 0
views.py
#ajax_required
#require_POST
#login_required
def post_like(request):
post_id = request.POST.get('id')
action = request.POST.get('action')
if post_id and action:
try:
post = Post.objects.get(id=post_id)
if action == 'like':
post.users_likes.add(request.user)
else:
post.users_likes.remove(request.user)
return JsonResponse({'status': 'ok'})
except:
pass
return JsonResponse({'status': 'error'})
urls.py
from django.urls import path, include
from . import views
app_name = 'posts'
urlpatterns = [
path('like/', views.post_like, name='like'),
]
index.html
{% with total_likes=post.users_likes.count users_likes=post.users_likes.all %}
<div class="post-info">
<div>
<span class="count">
<span class="total">{{ total_likes }}</span>
like{{ total_likes|pluralize }}
</span>
<a href="#" data-id="{{ post.id }}" data-action="{% if request.user in users_likes %}un{% endif %}like" class="like">
{% if request.user not in users_likes %}
Like
{% else %}
Unlike
{% endif %}
</a>
</div>
</div>
<div class="post-likes">
{% for user in users_likes %}
<div>
<p>{{ user.first_name }}</p>
</div>
{% empty %}
<p>No body likes this post yet</p>
{% endfor %}
</div>
{% endwith %}
<p>{{ post.id }}</p>
<a class="normal-text" href="{{ post.get_absolute_url }}">Discover more...</a>
</div>
</div>
ajax code
<script>
{% block domready %}
$('a.like').click(function(e){
e.preventDefault();
$.post("{% url 'posts:like' %}",
{
id: $(this).data('id'),
action: $(this).data('action')
},
function(data){
if (data['status'] == 'ok')
{
var previous_action = $('a.like').data('action');
// toggle data-action
$('a.like').data('action',
previous_action == 'like' ? 'unlike' : 'like');
// toggle link-text
$('a.like').text(
previous_action == 'like' ? 'Unlike' : 'Like');
// update total likes
var previous_likes = parseInt(
$('span.count .total').text());
$('span.count .total').text(previous_action == 'like' ? previous_likes + 1 : previous_likes -1);
}
}
);
});
{% endblock %}
</script>
I don't understand where the problem is... I thought in the url but I don't think so
From the documentation, it appears that your urls.py is at fault - you define a "like/" pattern, but there's nothing to tell the router that you mean for it to have a "posts/" prefix.
The examples given there explicitly use an include directive to do that, and you do no such thing. Try:
from django.urls import include, path
from . import views
urlpatterns = [
path('posts/', include([
path('like/', views.post_like, name='like'),
# Other posts/... endpoints
])),
# path('users/', include([ ...
]

How can I avoid repetition of code within a function-based view in Django?

I have been researching how can I avoid using snippet of code over and over. The answer probably will involve using (generic) Class-based functions. However, I am a beginner in Django and this seems confusing. Here is my view in views.py:
#login_required(login_url='/login')
def view_list(request, listing_id):
bid = Bid.objects.all().filter(listing=listing_id).order_by('-id')
b_u = bid[0].user
listing = Listing.objects.get(pk=listing_id)
if request.method == "GET":
return render(request, "auctions/view_list.html", {
"form": BidForm(),
"total_bids": bid.count(),
"bid": None if bid == 0 else bid[0].bid,
"listing": listing,
"bid_user": "Your bid is the current bid." if request.user == b_u else None
})
else:
form = BidForm(request.POST)
if form.is_valid():
value = form.cleaned_data
if value['bid'] <= bid[0].bid:
error_check = True
return render(request, "auctions/view_list.html", {
"error_check": error_check,
"alert": f"Your bid is lower than the current bid $({bid[0].bid})! Try placing a higher one.",
"form": BidForm(),
"total_bids": bid.count(),
"bid": None if bid == 0 else bid[0].bid,
"listing": listing,
"bid_user": "Your bid is the current bid." if request.user == b_u else None
})
else:
error_check = False
new_bid = form.save(commit=False)
new_bid.user_id = request.user.id
new_bid.listing_id = listing.id
new_bid.save()
return render(request, "auctions/view_list.html", {
"error_check": error_check,
"alert": "Your bid was successfully placed!",
"form": BidForm(),
"total_bids": bid.count(),
"bid": None if bid == 0 else bid[0].bid,
"listing": listing,
"bid_user": "Your bid is the current bid." if request.user == b_u else None
})
And here is my template code:
{% extends "auctions/layout.html" %}
{% load humanize %}
{% load crispy_forms_tags %}
{% block body %}
{% if error_check == True %}
<div class="alert alert-warning" role="alert">
{{ alert }}
</div>
{% elif error_check == False %}
<div class="alert alert-success" role="alert">
{{ alert }}
</div>
{% endif %}
<div>
<h3>Listing: {{ listing.title }}</h3>
<img src="{{ listing.image }}" alt="Listings' Images">
<p>{{ listing.description }}</p>
{% if not bid %}
<strong>${{ listing.price|stringformat:"1.2f" }}</strong>
{% else %}
<strong>${{ bid|stringformat:"1.2f" }}</strong>
{% endif %}
<p> {{ total_bids }} bid(s) so far. {% if bid_user %} {{ bid_user }} {% endif %}</p>
<form method="POST" name="bidding" action="{% url 'view_list' listing.id %}">
{% csrf_token %}
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">$</span>
</div>
<div style="margin: 0; padding: 0 2px; height: 6px;">
{% crispy form %}
</div>
<div class="input-group-append" >
<span class="input-group-text">.00</span>
</div>
<input type="submit" class="btn btn-primary" value="Place Bid">
</div>
</form>
<h4>Details</h4>
<li>Listed by: {{ listing.user }} </li>
<li>Category: {{ listing.category }} </li>
<li>Listing created at: {{ listing.created_at }} </li>
</div>
{% endblock %}
So how can I avoid all this repetition and make the code more succinct. Also, this way when the user places a successful bid, the rendered template does not contain the new information from the form.
The pattern is very simple
def some_view(request):
form = SomeForm(request.POST or None)
if request.method == 'POST' and form.is_valid():
# Form processing
return render(request, "auctions/view_list.html", {
"form": form
})

Django-Ajax giving error "Method Not Allowed (POST): /post/like/"

I am new to django i am using ajax and django for very first time. I have tried by best to search here and there but i couldn't get to solution
this answer didn't help as well
Django and ajax request Method Not Allowed (POST)
the error i am getting is
Method Not Allowed (POST): /post/like/
[07/Jun/2019 16:06:16] "POST /post/like/ HTTP/1.1" 405 0
below are the codes
likes_section.html
{% if request.user.is_authenticated %}
<form action="{% url 'like_post' %}" method="post">
{% csrf_token %}
{% if is_liked %}
<span class="mr-2" style="color:black;">{{ post.total_likes }} Like{{ post.total_likes|pluralize }}<button type="submit" id="like_the_post_by_user" class="btn btn-primary ml-2" name="post_id" value="{{ post.id }}">DisLike</button></span>
{% else %}
<span class="mr-2" style="color:black;">{{ post.total_likes }} Like{{ post.total_likes|pluralize }}<button type="submit" id="like_the_post_by_user" class="btn btn-primary ml-2" name="post_id" value="{{ post.id }}">Like</button></span>
{% endif %}
</form>
{% else %}
<span class="mr-2">{{ post.total_likes }} Like{{ post.total_likes|pluralize }}<button type="submit" id="like_the_post_by_user" class="btn btn-primary ml-2" name="post_id" value="{{ post.id }}" disabled>Like</button>Please Login to enable Like button</span>
{% endif %}
Ajax
$(document).ready(function(event){
$(document).on('click',"#like_the_post_by_user", function(event){
event.preventDefault();
console.log($("#like_the_post_by_user").val())
console.log("from jquery section")
var pk = $(this).attr('value');
$.ajax({
type : "POST",
url : "{% url 'like_post' %}",
data : {'id': pk , "csrfmiddlewaretoken": '{{ csrf_token }}' },
dataType : 'json',
success : function(response){
$('#like-section_user').html(response['form'])
console.log($('#like-section_user').html(response['form']));
},
error : function(rs, e){
console.log(rs.responseText);
}
});
});
});
urls.py
urlpatterns = [
path('', PostListView.as_view(),name="blog-home"),
path('post/<int:pk>/', PostDetailView.as_view(),name="post-detail"),
path('post/new/', PostCreateView.as_view(),name="post-create"),
path('post/<int:pk>/update/', PostUpdateView.as_view(),name="post-update"),
path('post/<int:pk>/delete/', PostDeleteView.as_view(),name="post-delete"),
path('user/<str:username>/', UserPostListView.as_view(),name="user-posts"),
path('post/<str:category>/', CategoryListView.as_view(),name="category-posts"),
path('about/', AboutListView.as_view(),name="about"),
#path('users/myposts/', ActiveUserPostDetailView.as_view(),name="my-blogs"),
path('feedback-email/', views.feedback_email,name="feedback-email"),
path('post/like/', views.like_post,name="like_post"),
]
views.py
def like_post(request):
#post = get_object_or_404(Post,id=request.POST.get("post_id"))
if request.method == 'POST':
print('method is {}'(request.method))
print("\ninside like view\n")
print("\n in {} \n".format(request.POST.get('id')))
post = get_object_or_404(Post,id=request.POST.get("id"))
is_liked = False
if post.likes.filter(id=request.user.id).exists():
print("\ninside like\n")
post.likes.remove(request.user)
is_liked = False
else:
print("\ninside dislike\n")
post.likes.add(request.user)
is_liked = True
comments = Comment.objects.filter(post=post,reply=None).order_by("-id")
context = {
"post":post,
"is_liked":is_liked,
"comment": comments
}
#return redirect("post-detail",pk=request.POST.get("post_id"))
print("\ngetting in ajax\n")
if request.is_ajax():
print("\ninside ajax\n")
html = render_to_string('blog/likes_section.html', context, request=request)
return JsonResponse({"form":html})
any help will be greatly appreciated !
Thanks in advance
Your "/post/like" URL is matching the URL pattern for CategoryListView, since it is "post" plus a string.
As you have done with the post detail view, bring the pattern for the like view earlier in the list of URLs so that it matches first.

Restrict each user to only vote once (Polls, django, python)

I found an similar question here, but unlike there and unlike in django official tutorial , I don't have a separate Choice class. How can I restrict every user to vote just one? What should I change in my code?
my models.py:
from django.contrib.auth.models import User
class Law(models.Model):
#some code here
yes_votes = models.IntegerField(default=0)
no_votes = models.IntegerField(default=0)
class Voter(models.Model):
user = models.ForeignKey(User)
law = models.ForeignKey(Law)
my views.py:
class LawDetailView(generic.DetailView):
model = Law
template_name = 'law_detail.html'
def get_queryset(self):
"""
Excludes any petitions that aren't published yet.
"""
return Law.objects.filter(pub_date__lte=timezone.now())
class LawResultsView(generic.DetailView):
model = Law
template_name = 'law_results.html'
def law_yes_vote(request, law_id):
if Voter.objects.filter(law_id=law_id, user_id=request.user.id).exists():
return render(request, 'law_detail.html', {
'law': p,
'error_message': "Sorry, but you have already voted."
})
else:
p = get_object_or_404(Law, pk=law_id)
p.yes_votes += 1
p.save()
return HttpResponseRedirect(reverse('laws:law_results', args=(p.id,)))
def law_no_vote(request, law_id):
if Voter.objects.filter(law_id=law_id, user_id=request.user.id).exists():
return render(request, 'law_detail.html', {
'law': p,
'error_message': "Sorry, but you have already voted."
})
else:
p = get_object_or_404(Law, pk=law_id)
p.no_votes += 1
p.save()
return HttpResponseRedirect(reverse('laws:law_results', args=(p.id,)))
my law_detail.html:
{% if request.user.is_authenticated %}
{% if error_message %}
<h1 >{{ error_message }}</h1>
{% else %}
<div class="row" id="row-voting">
<form action="{% url 'laws:law_yes_vote' law.id %}" method="post">
{% csrf_token %}
<button class="btn btn-success" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" >
<label >YES</label>
</form>
<form action="{% url 'laws:law_no_vote' law.id %}" method="post">
{% csrf_token %}
<button class="btn btn-danger" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" >
<label >NO</label>
</form>
</div>
{% endif %}
{% else %}
<h1>Please, register</h1>
{% endif %}
It looks like you have forgotten to create the voter instance after the user has voted.
def law_yes_vote(request, law_id):
if Voter.objects.filter(law_id=law_id, user_id=request.user.id).exists():
return render(request, 'law_detail.html', {
'law': p,
'error_message': "Sorry, but you have already voted."
})
else:
p = get_object_or_404(Law, pk=law_id)
p.yes_votes += 1
p.save()
Voter.objects.create(law_id=law_id, user_id=request.user.id)
return HttpResponseRedirect(reverse('laws:law_results', args=(p.id,)))
You'll need to update law_no_vote in the same way.

NoReverseMatch at /why is this occuring?

I had this error before but I couldn't fix it, so I moved back to previous git state. Now I have to fix this, why does this error keep happening? And what does this even mean? It's occuring from index.html {% url vote_for_post %} Here's my code, thanks in advance.
views.py
def index(request):
categories = Category.objects.order_by('likes')
latest_posts = list(Post.objects.order_by('pub_date')[:50])
hot_posts = sorted(latest_posts, key=lambda x: x.hot(), reverse=True)
controversial_topics = sorted(latest_posts, key=lambda x: x.controversy(), reverse=True)
context_dict = {
'latest_posts': latest_posts,
'categories': categories,
'hot_posts': hot_posts,
'controversial_topics':controversial_topics
}
return render(request, 'main/index.html', context_dict)
def vote_for_post(request, category_name, post_id):
category = get_object_or_404(Category, name=category_name)
post = get_object_or_404(Post, id=post_id, category=category)
if request.POST['type'] == 'upvote':
post.upvotes += 1
if request.POST['type'] == 'downvote':
post.downvotes += 1
post.save()
if request.POST['referer'] == 'index':
return HttpResponseRedirect(reverse('main:index'))
if request.POST['referer'] == 'category':
return HttpResponseRedirect(reverse('main:category', args=(category.name,)))
urls.py
urlpatterns = [
url(r'^$', views.index, name='index'),
#url(r'^add_post/', views.add_post, name='add_post'),
url(r'^add_post/$', PostCreateView.as_view(), name='post-add'),
url(r'^vote/$', views.vote_for_post, name='vote_for_post'),
url(r'^(?P<slug>[\w|\-]+)/edit/$', PostUpdateView.as_view(), name='post-edit'),
url(r'^(?P<slug>[\w|\-]+)/delete/$', PostDeleteView.as_view(), name='post-delete'),
url(r'^add_category/', views.add_category, name='add_category'),
url(r'^(?P<slug>[\w|\-]+)/$', views.post, name='post'),
url(r'^category/(?P<category_name_slug>[\w\-]+)/$', views.category, name='category'),
]
index.html
<div class="row placeholders">
<div class="col-sm-8">
<div class="row">
{% if hot_posts %}
{% for vote in hot_posts %}
<article>
<div class="vote">
<form method="post" style="display: inline"
action="{% url vote_for_post %}">
<input type="hidden" value="upvote" name="type">
<input type="hidden" value="index_page" name="referer">
<button type="submit">+</button>
{% csrf_token %}
</form>
<form method="post" style="display: inline"
action="{% url 'main:vote' vote.category.name vote.id %}">
<input type="hidden" value="downvote" name="type">
<input type="hidden" value="index_page" name="referer">
<button type="submit">-</button>
{% csrf_token %}
</form>
</div>
</article>
<hr>
{% endfor %}
{% else %}
<p>No topics are available.</p>
{% endif %}
</div>
</div>
</div>
As I see, "add_post", "vote" and "add_category" are ambiguous with your "slug" due to regex match.
url(r'^(?P<slug>[\w|\-]+)/$', views.post, name='post'),
url(r'^vote/$', views.vote_for_post, name='vote_for_post'),
url(r'^add_post/$', PostCreateView.as_view(), name='post-add'),
url(r'^add_category/', views.add_category, name='add_category'),
Try to give a better identifier for your urls.
e.g.
url(r'^(?P<slug>[\w|\-]+)/$', views.post, name='post'),
url(r'^vote/post/$', views.vote_for_post, name='vote_for_post'),
url(r'^add/post/$', PostCreateView.as_view(), name='post-add'),
url(r'^add/category/$', views.add_category, name='add_category'),
Let me know if this helped.
From the Django documentation: https://docs.djangoproject.com/en/1.9/ref/templates/builtins/#url
{% url 'some-url-name' v1 v2 %}
In your code you should add ' ' to the name in the url template tag.
<form method="post" style="display: inline" action="{% url vote_for_post %}">
should be:
<form method="post" style="display: inline" action="{% url 'vote_for_post' %}">
Concerning the NoReverseMatch at / u'main' is not a registered namespace.
Remove the 'main:' in your (reverse('main:index')) and reverse('category', args=(category.name,)) in your views.py
if request.POST['referer'] == 'index':
return HttpResponseRedirect(reverse('index'))
if request.POST['referer'] == 'category':
return HttpResponseRedirect(reverse('category', args=(category.name,)))

Categories