How to send text by template via url in Django? - python

I want to send text to a view via template. I have two different types of clients that will be processed differently, to take advantage of code I put it in a single view and the specific part treated it with an if else.
In the template:
Client prime
Client
In the urls.py
....
path('client/<str:typeclient>', Client, name='client'),
.....
In the view:
def Client(request, typeclient):
...
if typeclient == "prime":
...
else:
....
However I get the following error:
NoReverseMatch at /
Reverse for 'client' with no arguments not found. 1 pattern(s) tried: ['client\\/(?P<typeclient>[^/]+)$']
Apparently the text is not passing as a parameter that I inserted in the url.
In this sense, how can I pass a text from the template via url?

try this
path('client/<typeclient>', Client, name='client'),
Client prime
Client
Read this https://docs.djangoproject.com/en/3.1/topics/http/urls/
https://docs.djangoproject.com/en/3.1/ref/templates/builtins/#url

Related

Issue on Django Url Routers

I an trying to make url router in Django which supports following URLs :
http://localhost:8000/location/configuration
http://localhost:8000/location/d3d710fcfc1391b0a8182239881b8bf7/configuration
url(r'^locations/configuration$',
location_config.as_view(), name="location-config"),
url(r'^locations/(?P<location_key>[\w]+)/configuration$',
location_config.as_view(), name="location-config-uri")
Whenever I tried to hit http://localhost:8000/location/configuration, it picked up the second URL routing format instead of picking up first one.
Error:
TypeError at /locations/configuration/ get() missing 1 required
positional argument: 'location_key'
Can anyone help me here what goes wrong with the url routing format?
Nope, it does pick the first pattern which has no arguments, however you're using the same view in both patterns and location_config view has required argument location_key which is not provided when first pattern matches the URL. That's what error message is saying.
So write another view which will not require location_key argument or alter this view definition: add default to the parameter
def location_config(request, location_key=None):
....
now it is not a "required positional argument".
django Will look for a pk when you are using a detail view by default. you have to override it by using get_object()
in your case
def get_object(self, queryset=None):
location_key = self.kwargs.get('location_key')
obj = Model.objects.get(id=location_key)
return obj

Pass arguments between Webapp2 redirects

I want to redirect URL without slug, to the one with slug, at the urls.py level.
My endpoints looks as follows:
(r'/invoices/<:(-?\d+)>/print/', PrintHandler, 'ShortPrintHandler')
(r'/invoices/<:(-?\d+)>/<:([\w-]*)>/print/', PrintHandler, 'FullPrintHandler')
Is there any way I can pass first, decimal, argument from short URL to the long one, on redirect? Generating URLs without slug is already covered at handler level.
Tried to handle it with
RedirectRoute(r'/invoices/<:(-?\d+)>/print/', PrintHandler, redirect_to_name='FullPrintHandler')
But an error was thrown:
KeyError: 'Missing argument "1" to build URI.'
You can't do that with just a RedirectRoute; you need to get the slug value from somewhere.
You'll need to write a standard route, and in the handler you should get the object from the datastore and return a redirect to the full path using the slug.
Something like (untested):
class RedirectToFullPath(webapp2.RequestHandler):
def get(self, invoice_id):
invoice = Invoice.get_by_id(invoice_id)
self.redirect_to('FullPrintHandler', invoice_id, invoice.slug)

How can I get get_absolute_url to render a string in views.py in Django

I am trying to send an email with HTML content. The email would include a link to a DetailView of a specific instance of a model.
So in the "post" part of view which sends the email, I have something like:
html_content = ''+str(text)+''
The email that gets sent is:
my instance name
How do I render an HTTP string in views.py?
Thank you
albar was correct. I had tried get_absolute_url with and without parenthesis but never without parentheses and without the str() function. It did not occur to me to not use the str() function.

How to pass URL arguments after # sign in Django

If I send a link to a Django page which has #args in the URL, e.g.
http://localhost/someurl/?arg1=true&arg2=false#1970-01-01/2038-01-01/something_else
Then the person is not logged in - the login form appears. Unfortunately after successful login, it will redirect the person to:
http://localhost/someurl/?arg1=true&arg2=false
What I would like to keep somehow is also the #1970-01-01/2038-01-01/something_else
How can I do that in Django?
I would try modifying the code responsible for generating the link such that the # symbol is replaced by %23 (i.e. its percent encoding: https://en.wikipedia.org/wiki/Percent-encoding ).
The final URL would be:
http://localhost/someurl/?arg1=true&arg2=false%231970-01-01/2038-01-01/something_else

Why am I getting this Error in Django notification?

I'm using this one: https://github.com/pinax/django-notification/blob/master/docs/usage.txt
So, I followed all the steps.
from notification import models as notification
#first, create the notification type.
notification.create_notice_type("comment_received", ("Comment Received"), ("You have received a comment."))
#then, send the notification.
notification.send(request.user, "comment_received", {})
Of course, in my template directory, I created "notification", just like the doc says.
Inside /templates/notification/comment_received, I have 4 files:
full.txt, short.txt, notice.html, full.html
These files are blank right now. They just say a random sentence.
Why am I getting this error when I try to send the notification?
Exception Type: NoReverseMatch at /
Exception Value: Reverse for 'notification_notices' with arguments '()' and keyword arguments '{}' not found.
Did you include the proper URL configurations? Looks like Django can't find notification_notices in any of your urlconfs...
https://github.com/pinax/django-notification/blob/master/notification/urls.py
You should reference these in your site's urls.py, e.g.:
urlpatterns = patterns('',
(r'^notification/', include(notification.urls)),
...
You will need to create an entry in your urls.py file including the django-nofication urls.py file:
(r'^notifications/', include('notification.urls')),
See the Django docs for more information on including other urls.py files.

Categories