I am building a website with a simple blog.
I follow the instructions from: http://lightbird.net/dbe/blog.html
I got to a point where I see posts, but then he adds links to each post.
I added :
(r"^(\d+)/$", "post"),
to my urls.py and when I added : Comments everything breaks. It's like it's not sending the value. I think I am doing something wrong with the links..
Can anyone check my app?
Admin account : admin/admin.
Wrong code is in: templates/news/list.html
When I delete that line it works.
Here is my code: https://db.tt/b7qpib28
TRACEBACK : http://dpaste.com/1471932/
You need to remove the $ in url pattern that includes news.urls, so in Uploader/uploader/uploader/urls.py change the line 32:
(r"^news/$", include('news.urls')),
by this
(r"^news/", include('news.urls')),
That's all ;) ...this obstruct the rest of url, you can display the url for news.views.main because it didn't add anything to the url but news.views.post need to add the pk parameter
Firstly, your closing single quote is I the wrong place. Secondly, try using the url pattern name instead of the path to the view.
Try the following:
{% url 'post' post.pk %}
Related
My first question, so please be patient if I miss key info or not format it correctly.
I recently started experimenting with django and run into the following error:
Reverse for 'dispatcher' with arguments '(u'to-do',)' and keyword arguments '{}' not found
the relevant line in the template.html (I am showing a menu) is:
<li>{{ item }}</li>
I also includes {% load url from future %} as I read in some other question on this forum.
I have few other items in the list, such as 'today', 'tomorrow' etc. and as long as I don't have a hyphen I don't get any error. If I remove the hype, i.e. instead of to-do use todo, everything works.
snippet of my urls.py is:
urlpatterns = patterns('app.views',
url(r'^$', 'home', name="home"),
url(r'^items/(?P<item>\w+)/$', 'dispatcher', name="dispatcher"),
)
Whether relevant or not, I tracked down to iri_to_url function in encoding.py and saw that hyphen is not part of 'safe'list. But adding '-' to the list didn't solve the problem.
Appreciate your advise and help.
Your regex does not support the - Hence the error. change \w+ to [\w-]+
Try this:
url(r'^items/(?P<item>[\w-]+)/$', 'dispatcher', name="dispatcher")
There's some weird stuff happing with the URL reversal code in django 1.4.
I have a view called settings.views.app_view. I have viewed the page by typing in the URL manually to verify that the basic URL pattern is working.
url(r'^app/$', 'settings.views.app_view', name='settings_app_view'),
I have reversed the URL in a template and it works.
{% url settings_app_view %}
So, the URL pattern works, and I can call get the URL in a template, click the link and view the correct page.
So why can't I get the URL in a view using reverse()? All the code is clearly there, and not only that, it's clearly configured and working correctly as I've seen the page and reversed the URL in a template.
I have to be missing something small; does anyone know what it is?
ViewDoesNotExist at /settings/app/
Exception Value: Could not import settings.views.app_view. View does not exist in module settings.views.
# The highlighted code
url = reverse("settings_app_view")
Where exactly in your code does reverse() get executed? If reverse() gets executed during importing the python file, you can get a recursive import. Unfortunately a recursive import can have different results: AttributeError can happen on modules that should have this attribute....
See: https://docs.djangoproject.com/en/dev/ref/urlresolvers/#reverse-lazy
I have problem with url tag. I want to redirect to a function that is in for eg
project_name.forum.views.function. Here is how i try to create url
{% url forum.views.function %}
it gives me this error:
Caught ViewDoesNotExist while rendering: Tried forum in module project_name.forum.views. Error was: 'module' object has no attribute 'forum'
I added this url in urls.py(I can access it directly) What am I doing wrong?
The url tag is used to reference named urls. E.g.
url(r'^$',
login_required(views.user_babies),
name='babystats_user_babies'),
Then you use {% url babystats_user_babies %} (the url pattern name, not the view name)
It sounds more like an incorrectly set up URL conf. You get that error when you specify a view that doesn't exist.
The url tag failing gives you a failure to reverse url with params... message.
What does your URL conf look like? Does project_name.forum.views.forum exist?
I mean, I find it odd you can visit the page at all, but that's the first place I'd look.
I have seen this error before with django url reversing stemming from the urlconf being set up with a root like projectname.app.views.view instead of app.views.view, so it chokes on the reverse without the name of the project.
Another common issue would be that the url takes an extra parameter that can be blank, and it needs you to pass an empty string or whatnot.
I trying to implement the url template tag into my project.
I have a button that allows the user to save the data he is seeing.
So the url of this button is this:
(2)url(r'^nameviews/download/$', 'my.path.is.this.to.download' name="load"),
template:
Download
the url of the page that shows the information, and where the button is located is:
(1)(r'^nameviews/$', path.to.page),
but when I tried to click on the button (it should appear the url 2)it doesn't open the file with the data but instead gives me the same url that the main page (1)
the html validator gives me a error on the
<a href="">
It seems it doesn't recognize the url tag.
Anyone has any idea?
Resolved! I didn't really understood what's happen because I didn't change much but should have been some silly mistake.
Sorry!
Thanks any way :)
EDIT
Be careful with the order of the urls. At urls.py try this order:
url(r'^nameviews/download/$', name_of_view, name="load"),
url(r'^nameviews/$', name_of_view, name="first page"),
name_of_view is equal if the view is the same
I'm having some trouble with the django generic object_list function's pagination not really being "smart" enough to compensate my daftness.
I'm trying to do a url for listing with optional arguments for page number and category.
The url in urls.py looks like this:
url(r'^all/(?:(?P<category>[-\w]+)/page-(?P<urlpage>\d+))?/$',
views.listing,
),
The category and urlpage arguments are optional beacuse of the extra "(?: )?" around them and that works nicely.
views.listing is a wrapper function looking like this( i don't think this is where my problem occurs):
def listing(request,category="a-z",urlpage="1"):
extra_context_dict={}
if category=="a-z":
catqueryset=models.UserProfile.objects.all().order_by('user__username')
elif category=="z-a":
catqueryset=models.UserProfile.objects.all().order_by(-'user__username')
else:
extra_context_dict['error_message']='Unfortunately a sorting error occurred, content is listed in alphabetical order'
catqueryset=models.UserProfile.objects.all().order_by('user__username')
return object_list(
request,
queryset=catqueryset,
template_name='userlist.html',
page=urlpage,
paginate_by=10,
extra_context=extra_context_dict,
)
In my template userlist.html I have links looking like this (This is where I think the real problem lies):
{%if has_next%}
<a href=page-{{next}}>Next Page> ({{next}})</a>
{%else%}
Instead of replacing the page argument in my url the link adds another page argument to the url. The urls ends up looking like this "/all/a-z/page-1/page-2/
It's not really surprising that this is what happens, but not having page as an optional argument actually works and Django replaces the old page-part of the url.
I would prefer this DRYer (atleast I think so) solution, but can't seem to get it working.
Any tips how this could be solved with better urls.py or template tags would be very appreciated.
(also please excuse non-native english and on the fly translated code. Any feedback as to if this is a good or unwarranted Stack-overflow question is also gladly taken)
You're using relative URLs here - so it's not really anything to do with Django. You could replace your link with:
Next Page> ({{ next }})
and all would be well, except for the fact that you'd have a brittle link in your template, which would break as soon as you changed your urls.py, and it wouldn't work unless category happened to be a-z.
Instead, use Django's built-in url tag.
Next Page> ({{ next }})
To make that work, you'll have to pass your category into the extra_context_dict, which you create on the first line of your view code:
extra_context_dict = { 'category': category }
Is /all/a-z/page-1/page-2/ what appears in the source or where the link takes you to? My guess is that the string "page-2" is appended by the browser to the current URL. You should start with a URL with / in order to state a full path.
You should probably add the category into the extra_context and do:
next page ({{next}})
"Instead of replacing the page argument in my url the link adds another page argument to the url. The urls ends up looking like this "/all/a-z/page-1/page-2/"
that is because
'<a href=page-{{next}}>Next Page> ({{next}})</a>'
links to the page relative to the current url and the current url is already having /page-1/ in it.
i'm not sure how, not having page as an optional argument actually works and Django replaces the old page-part of the url
one thing i suggest is instead of defining relative url define absolute url
'Next Page> ({{ next }})'