One of my urls in url.py goes like this is like this
path('<str:subject>/<str:agglevel>/<str:cust_cd>/',views.Customer.as_view())
cust_cd takes value from UI. cust_cd is basically customer name which is a string value.
Url works fine for single words for cust_cd. Ex:Google,Gmail etc. But when I give words with spaces like Ex:You tube I get 404 error. It says you%20tube not found. Am not able to figure out how to configure url so that it accepts space characters.
in your urls.py file
instead of
<str:cust_cd>
try this
<slug:cust_cd>
for more information check out this link: https://docs.djangoproject.com/en/3.0/topics/http/urls/#path-converters
Related
I am tryimg to add content to a URL previously served by forum software. The URL is
/portal/forums/showthread.php?t=12345
I have this in my urlconf, but it's not working:
url("^portal/forums/showthread.php?t=12345", thread),
I am just matching the whole string to a single view for now, but a way to pass the topic ID as an argument would also be handy. (Hopefully all the old URLs similar enough to match, without any funky querystrings)
You need to access GET (Query string) parameters like this:
def myview(request):
t = request.GET.get('t')
#rest of the code.
The GET parameters should not be a part of the URL.
Your URL then would look like this:
url("^portal/forums/showthread.php", thread), #You might want the $ sign at the end.
I have a django app set up consisting of ListViews, TemplateViews etc..
So, I just added a small templateview to it like so:
#views.py
class TermsTemplateView(TemplateView):
template_name = "terms.html"
#urls.py
url(r'^terms/$', TermsTemplateView.as_view(), name='terms'),
and in terms.html, I am using for linking:
Terms & Conditions
For some strange reason, I keep getting 404 on localhost/terms as follows:
404: No <model_name> found matching the query
I am baffled why this is happening all of a sudden. I have the same set up for "about", "thanks", "contact" pages, and they seem to display it with no problems.
..and the worst part is, if I modify the urls.py like so:
url(r'^/terms/$', TermsTemplateView.as_view(), name='terms'),
and then go to http://127.0.0.1:8000//terms/ - the page seems to be there.. I am surprised why this is so :(
Any help would enlighten me!
The / at the end is the culprit of your problems. localhost/terms doesn't match '^terms/$' regular expression, localhost/terms/ does.
You can make / at the end optional by using ?:
url(r'^terms/?$', TermsTemplateView.as_view(), name='terms'),
UPD: Note that there is a better solution to the problem, APPEND_SLASH:
When set to True, if the request URL does not match any of the
patterns in the URLconf and it doesn’t end in a slash, an HTTP
redirect is issued to the same URL with a slash appended.
Also see:
Why would you need a slash at the end of a URL?
django - url with automatic slash adding
Append Slashes to URLs in Django
Most views in my project accept an optional username parameter and if exists, filter the querysets passed to the templates for that user. So, for example:
the index view handles both the following url patterns:
'^$' # general index page
'^(?P<username>[-\w]+)/$' # index page for the user
'^photos/$' # photo index page
'^(?P<username>[-\w]+)/photos/$' # photos for that user
...
As there are a number of such applications, it doesn't seem very DRY to implement the same logic by replicating the patterns. I thought it may be possible to recursively include the main urls.py module, so I did this:
url(r'^(?P<username>[-\w]+)/', include('urls')),
My reasoning was that, when an other urls module is included, the matched pattern is removed from the path. So, I was hoping that
'^(?P<username>[-\w]+)/photos/$'
would become
'^photos/$'
when it is matched by the recursively included urls module, with the extra username parameter. But this caused the development server to die silently when a request is made.
The second approach I can think of is to write a middleware, which would match the pattern in the url, if exists, and add the viewed user to the request and remove the part that matches the username from the request path. But I don't want to mess with the path as this may have unpredictable results.
What would you recommend? Am I too picky for DRYness?
Thanks,
oMat
Just define the regex as a string in the same file and use the string concatenation already!
user_regex = r"^(?P<username>[-\w]+)/"
Then you can do regex '%s/photos$'%user_regex so that you keep the regex defined only once, very dry.
Altho', your reasoning for including the urls.py patterns within the url tag is right and I'm not sure why it failed. Perhaps some other error?
I am trying to get the following setup up going.
Flatpages: Where all my static sites are (like: about, contact,..)
Dynamic Pages:
Here I am trying to link from one of the Flatpages to a start site:
the regex in the url conf of this startsite I tried was:
(r'^myapp/start/(\d+)/$', 'mysite.views.def_that_should_just_show_hello_world'),
In the views I had:
def def_that_should_just_show_hello_world(request):
return HttpResponse("Hello experiment world")
If I go to
/myapp/ I get 404: No FlatPage matches the given query.
/myapp/start/ I get 404: No FlatPage matches the given query.
/myapp/start/1 I get
Exception Type: TypeError
def_that_should_just_show_hello_world takes exactly 1 argument (2 given)
I thought with this setup I would get "Hello experiment world" on EVERY page.
Where did I go wrong?
I dont understand the multiple sites approach in regexs.
What would I have to do to print hello world on all these sites?
And then, what would I have to do to display 1 image on all of these sites?
Thanks a lot for the help!
You regular expression has a matching group in it - the (\d+) bit.
This requires one or more numeric characters to appear at the end of the url for that view. If you do not include the number at the end, this regular expression will not match the url. (url matching works like any other regular expression matching).
When you do include the number, eg. /myapp/start/1 you then have another problem. Because there is a matching group, the part of the url in the brackets will be passed as another argument to your view. Views are always passed the request as their first parameter but in this case the '1' matched by the (\d+) is provided as a second argument. This is why you are hetting the TypeError in this case.
Django's documentation has a lot of information on how url dispatching works, read that through and see if that makes sense!
from your_app_name import views
from django.conf.urls import url
urlpatterns = [
url(r'^$',views.method_name,name ='index'),
path('admin/', admin.site.urls),
I'm finding it hard to understand what exactly is passed to the patterns method in Django.
You see, I usually have my urls.py as:
urlspatterns = patterns('example.views',
(r'/$','func_to_call'),
)
Then in func_to_call I would get everything I want from the request object by using request.path. However on a second take, it's really quite horrific that I'm ignoring Django's slickness for such a longer, less clean way of parsing - the reason being I don't understand what to do!
Let's say you have 3 servers you're putting your Django application on, all of which have a domain name and some variation like server1/djangoApplicationName/queryparams, server2/application/djangoApplicationName and server3/queryparams. What will the urlpattern get passed? The whole url? Everything after the domain name?
The URLconf regex sees only the path portion of the URL, with the initial forward-slash stripped. Query parameters are not matched by the URLconf, you access those via request.GET in your view. So you might write a pattern like this:
urlpatterns = patterns('myapp.views',
url(r'^myapp/something/$', 'something_view_func')
)
The documentation has more examples and details.