Can't find the problem
urls.py
path("office", views.office, name="office")
views.py
def office(request):
offices_list = Office.objects.all()
context = {'offices_list': offices_list}
return render(request, "auctions/office.html", context)
template
<li class="nav-item">
<a class="nav-link text-white " href="{% url 'office' %}">office</a>
</li>
Error:
Reverse for '' not found. '' is not a valid view function or pattern name.
had to delete html page and rebuild it.
worked like charm.
still do not know what the issue was .
Thank you all for your kind contribution !
Related
I am attempting to create a view that allows users to delete a build log. On the view that shows the delete button with a link to the delete page I am getting the error
Reverse for 'build-log-delete' with no arguments not found. 1 pattern(s) tried: ['post/(?P<pk>[0-9]+)/build\\-log/(?P<pkz>[0-9]+)/delete$']
If I understand this error correctly its because I am not passing paramaters in the url.
<a class="delete-btn" href="{% url 'build-log-delete' %}">Delete</a>
However I do not understand why I need to pass parameters in the URL as I am not passing any new values into the URL and if so what parameters I would pass. Do I have to re pass the previous two?
urls
path('post/<int:pk>/build-log/<int:pkz>/', views.BuildLogDisplay, name='build-log-view'),
path('post/<int:pk>/build-log/<int:pkz>/delete', views.BuildLogDelete, name='build-log-delete') #error
views
def BuildLogDisplay(request, pk, pkz ):
post = BuildLog.objects.filter(post_id=pk)
log = BuildLog.objects.get(pk=pkz)
context = {
'post':post, 'log':log
}
return render(request, 'blog/buildlog.html', context)
def BuildLogDelete(request):
context = { }
return render(request, 'blog/BuildLogDelete.html', context)
full template
<div class="row">
<article class="cars-article">
<div class="flex">
<img class="rounded-circle article-img" src="{{ log.author.profile.image.url }}" />
<div>
<a class="article-title">{{ log.title }}</a>
</div>
</div>
<br>
<div>
{% if log.author == user %}
<a class="update-btn" href=""> Update</a>
<a class="delete-btn" href="{% url 'build-log-delete' %}">Delete</a>
{% endif %}
</div>
<hr class="solid">
<p class="article-content">{{ log.content | safe}}</p>
</article>
</div>
There are multiple errors in you code. You are not passing args in BuildLogDelete view but in url you are using those arguments. So the correct view should look like this.
def BuildLogDelete(request,pk,pkz):
# delete code
# write here
Next mistake which i can see is you are assigning queryset rather than object for the post key in BuildLogDisplay view. You should assign object.
post = BuildLog.objects.get(post_id=pk)
Lastly your original error mentioned in the question is because your build-log-delete url expects two arguments i.e pk and pkz but you haven't passed them in template. So it should be like this.
<a class="delete-btn" href='{% url "build-log-delete" pk=post.post_id pkz=log.pk %}'>Delete</a>
I would highly suggest you to look for already given generic views like ListView, TemplateView, CreateView, UpdateView and DeleteView. This will prevent you from reinventing the wheel
Ref: Django Class Based Generic Views
I am creating a webapp where i have sections like home,profile,and on home page there are several posts which are written by users. But when i go to the posts and from there if i have to return back to home page,it says page not found. it takes me to the following link http://127.0.0.1:8000/post/4/home. but i want to go to http://127.0.0.1:8000/home. How to do this
<li> Profile </li>
<li> Logout </li>
<li> Password change </li>
<li> home
</li>
my views.py of homepage
def homep(request):
context = {
'posts':post.objects.all()
}
return render (request,'welcome.html',context)
my urls.py
path('',views.index,name='index'),
path('login',views.login_view,name='login'),
path('register',views.register,name="Login now"),
path('logout',views.logout_view,name='logout'),
path('profile', views.profile,name='profile'),
path('home',PostListView.as_view(),name='home'),
path('edit_profile',views.profile_change,name='profile_change'),
path('post/<int:pk>/',PostDetailView.as_view(),name ='post-detail'),
That is because it will each time append the values at the end. You can prevent that by prepending a slash, so href="/logout", but probably it is better to make use of the {% url … %} template tag [Django-doc]:
<li>Profile </li>
<li>Logout</li>
<li>Password change</li>
<li>home</li>
It is not a good practice to hardcode html links in the href , You can use Django template tags, for example
Home
where home is the name given in the urls.py file
for more info you can check here
where you can also send id's like
Post
Using Django, i am passing few list to be displayed on Index page. As such it is working, but i am getting following error in the log. I believe it is an issue but can't find the rootcause. Can someone please help? I have provided required codes i am using ..
18/Jun/2020 20:30:03] "GET / HTTP/1.1" 200 24496
Not Found: /NONE
[18/Jun/2020 20:30:03] "GET /NONE HTTP/1.1" 404 2180
app Views.py
def index(request):
########
ndtvlist = "a list"
toilist = "a list"
context={'ndtvlist':ndtvlist,'toilist':toilist}
return render(request,'news/index.html', context)
proj Urls.py
path('',views.index,name='index'),
path('news/',include('news.urls')),
path('admin/', admin.site.urls),
App urls.py
path('',views.index,name='index'),
path('market/',views.market,name='market'),
path('world/',views.world,name='world'),
<nav class="navbar navbar-expand-lg bg-light techfont">
<div class="container">
<div class="navbar-nav align-items-center">
<a class="navbar-brand bigbrand" href="{% url 'index' %}">TopNews</a>
<a class="nav-item nav-link" href="{% url 'news:market' %}">Market</a>
<a class="nav-item nav-link" href="{% url 'news:world' %}">World</a>
</div>
</div>
</nav>
I tried a lot to solve the problem, i spot a way to trigger it, it happen everytime an url has a path like:
'your_project'/urls.py
path('/', include('home.urls')),
this added / always trigger this problem.
The only way i could solve was with a hacky fix. Simply give Django what it is seeking for :p
'your_project'/urls.py
def none_fix(request):
return render(request,'home/home.html')
urlpatterns = [
path('None', none_fix)]
I don't know if there is a clean way to fix it, and i don´t like the solution. This error might spot a broken code i didn´t find yet.
In my case the problem was contains with broken images with url('None').
You may try to find 'None' value in your Website, if you couldn't find it continue search in DevTools > Network page
I am trying to generate an URL using regex and have got it to an extent. But, the problem is there is an 'x' in the URL which I don't want there, without it the URL is perfect. I have tried few changes on the regex but removing or adding anything throws an error.
views.py
def clothes_details(request,slug):
item = data.objects.get(slug=slug)
print(item)
return render(request,clothes_details.html,{'item':item})
urls.py
url(r'^(?P<designerlink>)[\w-]+/(?P<slug>[\w-]+)/$',views.clothes_details,name='clothes_details'),
item.html
<div class='items'>
{% for item in all_clothes %}
<div class='item'>
<a href="{% url 'clothes_details' designerlink=item.designerlink slug=item.slug %}">
<img src="{{item.itemimage.url}}" style="width:350px;height:450px;">
</a>
<p>{{item.itemcode}}</p>
<p>{{item.itemprice}}</p>
<p>{{item.itemname}}</p>
</div>
The result is getting 'http://127.0.0.1:8000/designernamex/dressname/' but I need to get 'http://127.0.0.1:8000/designername/dressname/'
I want to pass userid from django url to my view
Here is what I have written in Django template
<a href ={% url 'user_details' x.id %} class='btn btn-primary' style="float: right;" >Know More</a></div>
To handle this url I have written Url like
url(r'^User/(\d{userid})/$', 'search.views.user_detail',name='user_details'),
But I am getting an error i.e
NoReverseMatch at /search/
Reverse for ''user_details'' with arguments '(2L,)' and keyword arguments '{}' not found.
Please help me out What might I am doing wrong here .
No quote ''
<a href ={% url user_details x.id %} class='btn btn-primary' style="float: right;" >
Know More
</a>
Another your url
url(r'^User/(?P<userid>\d+)/$', 'search.views.user_detail', name='user_details'),
Be carefull, after Django 1.5, use must use quotes. I came across this solution and tried it, got an error. I'm using Django 1.6 and you need the quotes:
<a href ={% url 'user_details' x.id %} class='btn btn-primary' style="float: right;" >
Know More
</a>
hope it helps.