I'm suprised that I cannot access my product detail page through the url and I don't understand why since I've already done this basic thing plenty of times...
I have a page where all my products are displayed, when the user click on a product he is redirected to the product detail, that's it.
Somehow when I click a link linked to the product detail or type de correct path to the url it loads the same page where all the product are shown but it doesn't even call the product detail view, why so ?
Here are the views :
def rcdex(request):
list = Liste.objects.all()
return render(request, 'rcdex.html', {'list':list,})
def rc_detail(request, id):
list = Liste.objects.get(id=id)
return render(request, 'rc_detail.html', {'list':list,})
Here are the urls :
url(r'^', views.rcdex, name="rcdex"),
url(r'^rc/(?P<id>\d+)/$', views.rc_detail, name='rc_detail'),
Here is how I call the rc_detail view on the template :
<th>{{ l.entreprise }}</th>
I don't get why it doesn't show me the correct template (rc_detail.html) but instead reload rcdex.html ?
You haven't terminated your rcdex urlpattern, so it matches everything. You should use a $:
url(r'^$', views.rcdex, name="rcdex"),
you can also do like this..
url(r'^rc/(?P<id>\d+)/$', views.rc_detail, name='rc_detail'),
url(r'^', views.rcdex, name="rcdex"),
Related
So I'm creating a small ecommerce website for a friend and cannot work out why the url won't resolve itself.
For the sake of simplicity whilst making the website I'm using a product called "number 1". It has a slug field of "number-1" and on the product page clicking on the product takes a user to "/shop/number-1"
My url pattern for this is:
url(r'^<slug:url>', views.item, name='products')
with the view:
def item(request, url=""):
products = product.objects.get(url=url)
return render(request, 'shop\product.html', {'products', products})
As far as I can tell this should render my product.html template but instead it returns a 404 and I'm not sure why?
If it helps I have other views, such as product types set within the same views and they work fine so as far as I can tell its that the slug:url isn't being used in the views.item, or the view isn't getting the context properly.
Also I'm on django 1.11.7 for this project.
The url pattern you are trying to use (slug:url) is only valid in Django 2.
If you are on Django 1.11 then you need to use a regular expression - something like this:
url(r'^?P<url>[\w-]+', views.item, name='products')
Always make sure you're looking at the documentation for your version of Django ;-).
Please change url pattern to:
url(r'^?P<url>[\w-]+', views.item, name='products')
Firstly thanks for taking the time to read my problem
I tried to add a custom button to my admin changeList view, but it always gives me Page not found (404)
This my modelAdmin code:
class MyModelAdmin(admin.ModelAdmin):
class Media:
js = ('admin/js/additional_js.js',)
model = MyModel
def get_urls(self):
urls = super(MyModelAdmin, self).get_urls()
analyze_url = patterns('',
(r'^(?P<pk>\d+)/analyze/$',
self.admin_site.admin_view(self.analyze_view))
)
return analyze_url + urls
def analyze_view(self, request, pk):
# some code here
HttpResponseRedirect(
reverse('admin:myapp_MyModel_change', args=(pk,))
)
My jQuery code for adding custom link to change list view:
(function($) {
$(document).ready(function($) {
$(".object-tools").append('<li>Analyze</li>');
});
})(django.jQuery);
when I click my custom link it gives me this:
MyModel object with primary key '3/change/analyze' does not exist.
I see that the link does not point to the view
Can someone help me fix this issue.
Thanks
In Django 1.9, the admin change url has changed to /admin/<app>/<model>/<pk>/change/ (release notes).
Therefore, it doesn't make sense to link to href="analyze/" in your html. The relative url is treated as /admin/<app>/<model>/3/change/analyze, not /admin/<app>/<model>/3/analyze as you expect.
You can probably fix the problem by changing the link to:
<a href="../analyze/" ...
Ideally, it would be better to reverse the url rather than having a relative link. I think this would make the code less fragile.
Since you are using Django 1.9, you can update your code to use a list rather than patterns:
from django.conf.urls import url
analyze_url = [
url(r'^(?P<pk>\d+)/analyze/$', self.admin_site.admin_view(self.analyze_view)),
]
i want to create django project with two templates main list and details from the main list using genetic views List view and details views.
the main list work but not work details list.
this my code can somebody to help me ?
urls.py
urlpatterns = [
url(r'^$', ListView.as_view(
model = Test,
queryset = Test.objects.all(),
context_object_name = "test_list",
template_name='blog\test_list.html')),
url((r'^(?P<pk>\d+)-(?P<slug>[-\w]+)/$', DetailView.as_view(
context_object_name="test_list1",
model=Test,
template_name='blog\Test_details.html'
), name="test"),
]
html code test_list
{% for test in test_list %}
<h2> {{test.Title}}</h2>
{% endfor %}
html code test_details
<h2>{{ test.Title }}</h2>
Your link doesn't match your URLs: you're just passing the ID in your link, but the URL pattern is expecting the link and the slug.
I think your problem is a couple of typos:
First in urls, you should change this:
url((r'^(?P<pk>\d+)-(?P<slug>[-\w]+)/$', DetailView.as_view(
to this:
url((r'^(?P<pk>\d+)/$', DetailView.as_view(
Second, (again for the DetailView) your context_object_name is set to test_list1 but your template uses test (the two should match; considering there is no list in this detail view, I would change them to test).
Third, your template_name is set as blog\Test_details.html with the capital T while your html file is actually with lower case t
yeah that correct we success(just delete -(?P[-\w]+) ) to connect first and second template but the second template my Test.Title is empty not show me details from my database,if i use {%for%} show me error message
Django 1.8 / Python 3.4
I have a website.html that displays entries from my database, each of which is identified by its ID. Now, at the end of each displayed entry I have a link to an "edit" view, looking like this:
<td>edit</td>
The link is working fine and leads to the correct view:
def edit(request, object_id):
implemented in views.py. There some code is executed correctly too, and in the view's last line I have:
return redirect('website.html')
Obviously, after the chosen entry has been edited I want my website.html with the edited entry being displayed in the browser again: 127.0.0.1:8000/website/. However, what happens is that I get a Page not found (404) error, with the information:
Requested URL 127.0.0.1:8000/website/2/website.html
The "2" here is the ID of the entry.
Maybe I have the wrong idea about how redirects work, but I was assuming that instead of calling the respective view's url from url.py it would open the url provided in the redirect() function?!
What happens, though, is that this url gets appended to the view's url!
Here is my urls.py:
from django.conf.urls import include, url
from django.contrib import admin
from www.list.views import website, edit
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^website/$', website, name="website"),
url(r'^website/(?P<object_id>\d+)/$', edit, name="edit"),
]
I'm pretty sure the third url entry is causing the problem but I have absolutely no idea how to change it for the redirect to work. Also, the edit view doesn't have an actual website (template), but I'm supposed to provide a URL for it, so this was the best I could think of.
The way this should work is: Click the "edit" link on website.html, the code from the edit-view is being executed, and, afterwards, the website.html with the change in the database entry gets displayed again.
^^ How to achieve this? Any help is appreciated!
Redirect uses names or absolute URLS. You should either use the name of your URL:
return redirect('website') # since name="website"
or an absolute URL, like:
return redirect('/website/')
you can use the reverse function instead of redirect
from django.core.urlresolvers import reverse
return reverse('website')
I found the mistake and the solution:
At the end of the edit-view it's correct to write "return redirect('website')". However, just as I assumed, the URL of edit in urls.py was wrong.
Instead of
url(r'^website/(?P<object_id>\d+)/$', edit, name="edit"),
it should just be
url(r'^(?P<object_id>\d+)/$', edit, name="edit"),
Thank you nonetheless!
Project urls.py includes app urls. I am using HttpResponseRedirect to get Likes posted on site. I am not trying to call for template so this is why not using render_to_response. My app view is:
def like_article(request, article_id):
if article_id:
a = Article.objects.get(id=article_id)
count = a.likes
count += 1
a.likes = count
a.save()
return HttpResponseRedirect('articles/get/%s' % article_id)
My app urls.py reflects likes redirection like this:
url(r'^like/(?P<article_id>\d+)/$', 'article.views.like_article'),
My parent "articles" HTML file extended from base says:
<p>{{article.likes}} people liked this article</p>
My single article page extended from base.html shows:
<p>Like</p>
Please advise.
You'd better use {% url [name] [parameters] %} in your template while reverse function in your view to create urls.
In your question, I think the problem is the url router doesn't match.
See:
<p>Like</p>
And:
url(r'^like/(?P<article_id>\d+)/$', 'article.views.like_article'),
It seemed the /article prefix doesn't appeared in you url.
Have you mapped the url - articles/get/article_id, i.,e added a similar pattern in urlpatterns (ex: url(r'^get/(?P<article_id>\d+)/$', 'article.views.get_article', name='get_article'),) tuple, to which you redirected the users!
If yes, then have you created a proper view for it!