I am new to Django and cant seem to find a solution to my problem
I get the following error
Reverse for 'todo_list' with arguments '()' and keyword arguments
'{'cid': 1}' not found. 1 pattern(s) tried: ['todo/(?P<cid>)/']
1 {% extends "base.html" %}
2 {% block nav_title %} Company Batches {% endblock nav_title %}
3 {% block content %}
4 <div class="jumbotron">
5
6 {% for obj in object_list %}
7 <a href={% url 'todo_list' cid=obj.company.id%} class="href-nostyle">
8 <div class="container">
9 <div class="jumbotron" style="background:white">
10 <div class="text-center">
11 <h1>{{ obj.company }}<br>
12 <small>{{ obj.job }}</small>
13 </h1>
14 </div>
15 </div>
16 </div>
17 </a>
This template is located in an app named company_batches and I am attempting to navigate a user to the todo app using an href
my url tag is
{% url 'todo_list' cid=obj.company.id%}
my main urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', home, name='home'),
url(r'^batches/', include('company_batches.urls')),
url(r'^todo/', include('todo.urls'), name='todo')
]
todo/urls.py
urlpatterns = [
url(r'^$', ToDoCreateView.as_view(), name='todo_create'),
url(r'^(?P<cid>)/', ToDoListView.as_view(), name='todo_list'),
]
the relevant views.py
class ToDoListView(ListView,):
template_name = 'todo/todo_list.html'
def get_context_data(self, *args, **kwargs):
context = super(ToDoListView, self).get_context_data(*args, **kwargs)
return context
def get_queryset(self, cid):
return ToDoList.objects.filter(company=self.cid)
I cant figure out what I'm doing wrong, some guidance would be much appreciated
There are a few things to notice here.
Regular expression, probably the actual issue here
Capturing the cid in your url regex does not contain a proper capturing group. Since it's an ID, you should only capture digits with \d+
url(r'^(?P<cid>\d+)/', ToDoListView.as_view(), name='todo_list'),
Closing url regex
The current url does not contain a closing sign. If the url actually ends after the /app/<id>/, you should most likely close the regular expression with the dollar sign $.
url(r'^(?P<cid>\d+)/$', ToDoListView.as_view(), name='todo_list'),
Namespace usage
You are using a name while including the todo app urls. To use the namespace properly, you should drop the name in the todo/ url and add
the namespace to the include.
url(r'^todo/', include('todo.urls', namespace='todo'))
Now in your template you can use the namespace
{% url 'todo:todo_list' cid=obj.company.id %}
Your regex is broken; it doesn't have any characters to match on. It looks like you want to capture a numeric PK, do it should be:
r'^(?P<cid>\d+)/
Related
Reverse for 'details' with arguments '('Federal Airports Authority of Nigeria (FAAN)',)' and keyword arguments '{}' not found.
1 pattern(s) tried:
['details/(?P<company_name>[0-9A-Za-z]+)/$']
This is my urls.py
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^details/(?P<company_name>[0-9A-Za-z]+)/$', views.details, name='details'),
url(r'^full_list/$', views.full_list, name='full_list' ),
]
This is the models.py
class CompanyDetail(models.Model):
name = models.CharField(max_length=300)
company_logo = models.FileField(default='')
company_info = models.TextField()
company_address = models.TextField()
tag = models.CharField(max_length=300)
def __str__(self):
return self.name
This is my views.py
def details(request, company_name):
company = CompanyDetail.objects.get(name=company_name)
return render(request, 'company_profiles/details.html',
{'company':company} )
def full_list(request):
lists = CompanyDetail.objects.all()
return render(request, 'company_profiles/full_list.html',
{'lists':lists})
This is the template :
{% extends 'company_profiles/base.html' %}
{% block content %}
{% for company in lists %}
<p>
<div class="alert alert-info" role="alert">
{{ company }}
</div>
</p>
{% empty %}
<p>No companies found</p>
{% endfor %}
{% endblock content %}
I only get no reverse match when there are spaces in the company name.
That's because your regex
(?P<company_name>[0-9A-Za-z]+)
doesn't allow for spaces in the company name. Django correctly tells you there is no reverse match.
Pick one of the options below:
Change the name validation code to disallow spaces (and migrate existing rows), or
Change the regex in urls.py to allow spaces
I recommend the second option.
You should try this in urls.py for match company name
(?P<company_name>[\w-]+)
and also one thing why you are not using Company ID as a parameter? like below:
in urls.py
(?P<company_id>[0-9]+)/
in templates
<a href="{% url 'company_profiles:details' company.pk %}"
class="alert-link">{{ company }}</a>
urls.py
urlpatterns = [
url(r'^$', views.gallery, name='home'),
url(r'^(?P<album_id>\d+)/(?P<pic_id>\d+)/$', views.details, name='details'),
url(r'^(?P<album_id>\d+)/', views.album_details, name='album_details'),
]
views.py
def details(request, pic_id):
picture = get_object_or_404(Picture, pk=pic_id)
print("accessed details %s" %picture)
context = {
"picture": picture
}
return render(request, "picture_details.html", context)
gallery_details.html
{% for picture in pictures %}
<div class="img">
<a href="{% url 'gallery:details' picture.id %}">
<img src="{{ picture.picture_thumbnail.url }}" />
</a>
<div class="desc">{{ picture.description|truncatewords:5 }}</div>
</div>
{% endfor %}
When i try to run this i get an exception value:
Reverse for 'details' with arguments '(3,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['gallery/(?P<album_id>\\d+)/(?P<pic_id>\\d+)/$']
It should load page with single image, but it doesn't. Don't know why.
You may want to rearrange your urls so that the less specific one is first
url(r'^(?P<album_id>\d+)$', views.album_details, name='album_details'),
url(r'^(?P<album_id>\d+)/(?P<pic_id>\d+)/$', views.details, name='details'),
You will also note that I've modified the first regex to include the $ character to indicate thats the end of the match
I'm having trouble passing a value from my Template index.html to
my View (duplicates(request, title).
Restricted to version 1.4.3.
I believe it to be a regex problem on my part in the urls.py file.
Current Error is Page not found (404).
urls.py
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'^missions/$', 'app.views.index'),
url(r'^missions/(?P<mission_id>\d+)/$', 'app.views.mission_overview'),
url(r'^missions/duplicates/(?P<title>[A-Za-z0-9-\s\>]+)/$',
'app.views.duplicates'),
)
I've tried using the title as a query string such as
url(r'^missions/duplicates/title(?P<title>[\s.\>])/$', ...)
index.html
{% if mt %}
<!-- Title (string), Num (int), mission_id (list) -->
{% for title, num, mission_id in mt%}
{% if num > 1 %}
<li><a href="missions/duplicates/{{ title|urlencode }}/">
{{ title }}
</a></li>
{% else %}
{% for mid in mission_id %}
<li>{{title}}</li>
{% endfor %}
{% endif %}
{% endfor %}
{% else %}
<p>No Titles</p>
{% endif%}
I have also tried
{{ request.GET.urlencode }}
and
{{% url app.views.duplicates title %}}
views.py Only showing the required inputs
def duplicates(request, title):
I either gotten Page not found errors or duplicates() takes exactly 2 arguments (1 given).
Main goal is getting title from the template to the view duplicates.
I have some funky titles like...
01 Wall_01-_Store>
AB.Chicken.1 StoreY
TO.Test.0 StoreZ'
Thanks ahead of time!
EDIT
The trailing slash was the problem here - the Page not found error shows url pattern 3 ending with a slash, but the current URL, does NOT end with a slash. I suspect some code changes between various bits copied and pasted here since the a href= line in the template DOES have a trailing slash.
The TypeError is caused by the fact that the named group is not matching anything - it requires AT LEAST ONE character, as the regex ends in + and not *. Why two trailing slashes are not needed here may also come down to code changes between bits that you've pasted up...
quite new to Django and Python. Hoping to get some help on what went wrong with the below code. Basically, I am trying to create a url with and id and a slugfield e.g. something.com/1/arts-and-crafts.
Stucked and getting a NoReverseMatch error below:
Reverse for 'category' with arguments '(2, u'arts-and-crafts')' and keyword arguments '{}' not found. 2 pattern(s) tried: ['(?P\d+)/[-\w]+/$', '(?P\d+)/$']
Below is the code that I am using, would greatly appreciate any help in understanding what went wrong:
template html
<ul class="list-unstyled">
{% for category in categories %}
{% if forloop.counter < 10 %}
<li><a href="{% url 'category' category.id category.slug %}">
{{ category.cat_name}}</a></li>
{% endif %}
{% endfor %}
</ul>
url.py
url(r'^(?P<cat_id>\d+)/[-\w]+/$', 'guides.views.category', name='category'),
views.py
def category(request, cat_id):
categories = Category.objects.all()
guides = Guide.objects.filter(category__id=cat_id).order_by("created_date").reverse()
is_featured = Guide.objects.filter(category__id=cat_id).filter(featured=True)
selected_category = get_object_or_404(Category, pk=cat_id)
return render(request, 'guides/category.html', {'categories': categories, 'guides': guides, 'is_featured': is_featured, 'selected_category': selected_category})
Your URL doesn't capture the slug as a parameter, so it can't be used in the reverse call. Either change the second pattern to (?P<slug>[-\w]+) or don't use category.id in the {% url %} tag.
I've looked at a lot of different posts, but they're all either working with a different version of django or don't seem to work. Here is what I'm trying to do:
urls.py (for the entire project):
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^blog/', include('blog.urls', namespace="blog")),
url(r'^admin/', include(admin.site.urls)),
)
urls.py (specific to the app):
urlpatterns = patterns ('' ,
url(r'^$', views.index, name='index'),
url(r'^(?P<slug>[\w\-]+)/$', views.posts, name="postdetail"),
)
views.py:
def index(request):
posts = Post.objects.filter(published=True)
return render(request,'blog/index.html',{'posts':posts})
def posts(request, slug):
post = get_object_or_404(Post,slug=slug)
return render(request, 'blog/post.html',{'post':post})
And finally the template:
{% block title %} Blog Archive {% endblock %}
{% block content %}
<h1> My Blog Archive </h1>
{% for post in posts %}
<div class="post">
<h2>
<a href="{% url "postdetail" slug=post.slug %}">
{{post.title}}
</a>
</h2>
<p>{{post.description}}</p>
<p>
Posted on
<time datetime="{{post.created|date:"c"}}">
{{post.created|date}}
</time>
</p>
</div>
{% endfor %}
{% endblock %}
For some reason this gives me a "No reverse Match": Reverse for 'postdetail' with arguments '()' and keyword arguments '{u'slug': u'third'}' not found. 0 pattern(s) tried: []
I've already tried getting rid of the double quotes around postdetail in the template, and I've also tried referring to it by the view name instead of the pattern name. Still no luck. The documentation isn't too helpful either.
Help is really appreciated! Thanks
You've used a namespace when including the URLs, so you probably need to use "blog:postdetail" to reverse it.