Why am I getting this Error in Django notification? - python

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.

Related

How to send text by template via url in Django?

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

Django optional parameter is not readed from url

I was reading the thread Django optional url parameters
And following the steps to generate a URL with a single optional parameter.
Well, my URL should be:
/client/
/client/?clientname=John
And I have defined two urlpatterns
url(r'^$', views.index, name='index'),
url(r'^/(?P<clientname>\d+)/',views.index),
Well, at this point both of them render the page.
But, in my view:
def index(request, clientname='noparameter'):
print("The searched name is: " + str(clientname))
The searched name is always noparameter
Am I doing something wrong?
Url you are having is
/client/John/
instead of
/client/?clientname=John
also even in the following example using John will fail as your regex is for digits , check out more on topic of django dispatcher
/client/4/
if you want to get GET parameters instead you can do that in view by using the following
request.GET.get('clientanme', None)
It seems as though you are getting confused between a keyword argument and a get request. Using keyword arguments, which your urls.py is configured for, your view would like this:
def index(request, **kwargs):
clientname = kwargs.get("clientname", "noparameter")
print("The searched name is: " + str(clientname))
Your urls.py would also have to change to this for the url to this:
url(r'^client/(?P<clientname>\w+)/',views.index),
This could be called in the browser like:
/client/John

Django NoReverseMatch for a Particular User

This is quite surprising and I can't seem to get my way around it.
The code below works for most users but it breaks when I try to render a link for user SSenior generating the error below:
NoReverseMatch at /tofollow/
Reverse for 'profile' with arguments '(u'SSenior ',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['user/(?P\w+)/$']
urls.py
url(r'^tofollow/$', views.tofollow, name='tofollow'),
url(r'^user/(?P<username>\w+)/$', views.profile, name='profile'),
template.html
#{{user.username}}
The username has a space in the end of it.
u'SSenior '
The regex \w+ does not match spaces, therefore you get the NoReverseMatch error.
Remove the space (you could use the Django admin to do this) and it will work.

Redirection is leading me to Page not found error in Django

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!

Caught NoReverseMatch while rendering, but have matching URL name

As the title implies I've got the NoReverseMatch error, but my url.py has the corresponding named url. I feel pretty confident I've missed something simple, but being new I can't seem to find my own problem.
ERROR: (Generated in the below employee_edit.html file)
Caught NoReverseMatch while rendering: Reverse for ''employee_new''
with arguments '()' and keyword arguments '{}' not found.
\home\username\mysite\myapp\views.py:
class v_EmployeeCreate(CreateView):
model = Employee
template_name = 'employee/employee_edit.html'
def get_success_url(self):
return reverse('employee_list')
\home\username\mysite\url.py:
from myapp.views import v_EmployeeCreate, v_EmployeeList
urlpatterns = patterns('',
< ... snip ...>
url(r'^newEmployee$', v_EmployeeCreate.as_view(), name="employee_new"),
)
\home\username\mysite\myapp\templates\employee\employee_edit.html (line 7):
<form action="{% url 'employee_new' %}" method="POST">
I feel like there is a file path issue, but I'm not sure how I would resolve that. The named URL works to get me to the template, but then the template itself fails to generate a url.
For the sake of documentation so far I have:
reloaded django
checked spelling
confirmed non-template functionality (Same setup without the template tags is fine. page loads)
Working from tutorial: http://effectivedjango.com/tutorial/views.html#creating-contacts
I think you are using an old version of Django - before 1.5 (current is 1.6). The clue is that your error message has two single-quotes around the view name: in those older versions, you shouldn't put quotes around the name in the url tag.
You should (preferably) upgrade Django, or (if you really can't do that) use {% url employee_new %}

Categories