Template-html
<p>
Inscreva-se
</p>
URLS.py
urlpatterns = [
url(r'^$', index, name = 'index'),
#url(r'^(?P<pk>\d+)/$', details, name = 'details'),
url(r'^(?P<slug>[\w_-]+)/$', details, name = 'details'),
url(r'^(?P<slug>[\w_-]+)/inscricao/$', enrollment, name = 'enrollment'),
]
Url does not find the path specified.
You have a space between the % and } at the end of the tag.
{% url 'courses:enrollment' courses.slug % }
^
Change it to
{% url 'courses:enrollment' courses.slug %}
Since you are using the namespace courses in your tag courses:enrollment, you should set app_name in your courses/urls.py file.
app_name = 'courses'
urlpatterns = [
...
]
You are using the word "courses" in your URL reversal, even though it was never defined. Try using just this: {% url 'enrollment' courses.slug % }
Related
I want to pass the argument to the function through the url. But I am getting an error as:
Reverse for 'combodetails' not found. 'combodetails' is not a valid
view function or pattern name.
urls.py:
app_name = "dashboard"
urlpatterns = [
path('combodetails/<int:id>/', combodetails, name="combodetails"),
]
views.py:
def combodetails(request, id):
print(id)
return render(request, "dashboard/combodetails.html", context={})
.html:
<a href="{% url 'dashboard:combodetails' model.id %}" class="icuwfbegb">
I have used the same method in other functions and they are working fine. But here I don't know what's happening.
In project.urls.py :
urlpatterns = [
path('dashboard/', include('dashboard.urls', namespace="dashboard"),),
]
please try this:
<a href="{% url 'combodetails' model.id %}" class="icuwfbegb">
I have commented on some parts of code in the HTML file using html comment style i.e. <!-- --> where I have used URL without argument that's why I am getting errors. I have removed the commented section from my file and now it's working.
I am following this tutorial https://tutorial.djangogirls.org/en/extend_your_application/ but getting Templatesyntax error when trying to pass pk from html to url using path method.
With what i have read about this error this has something to do with braces and quotes but in this case i am not able to figure out the exact problem with the syntax.
This the listview.html
{% for vehicle_list_load in vehicle_list_loads %}
<tr>
<td>{{vehicle_list_load.vehicle_num}}</td>
<td>{{vehicle_list_load.Driver_name}}</td>
<td>{{vehicle_list_load.BusinessUnit}}</td>
<td>{{vehicle_list_load.CheckinTime}}</td>
<td>{{vehicle_list_load.Type}}</td>
<td>
<a href= "{% url 'vehicle_movement:checkoutview' pk = vehicle_list_load.pk %}" class = "glyphicon glyphicon-pencil" aria-hidden ="true" > Edit</a>
</td>
</tr>
{% endfor %}
this is vehicle_movements urls.py
from django.urls import path
from vehicle_movement import views
app_name = 'vehicle_movement'
urlpatterns = [
path('checkoutview/<int:pk>/',views.checkout, name = 'checkoutview'),
]
this is main urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include(('vehicle_movement.urls','vehicle_movement'),namespace = 'vehicle_movement')),
]
This is the view
def listView(request):
vehicle_list_loads = list(Checkin.objects.all().filter(Type ='Loading'))
vehicle_list_unloads = list(Checkin.objects.all().filter(Type ='Unloading'))
current_time = datetime.utcnow().replace(tzinfo=utc)
diff = current_time
return render(request,'vehicle_movement/listView.html',
{'vehicle_list_loads':vehicle_list_loads,'vehicle_list_unloads':vehicle_list_unloads,'diff':diff})
on clicking on edit this view needs to open
def checkout(request,pk):
post = get_object_or_404(Checkin, pk= pk)
return render(request,'vehicle_movement/checkout.html',{'post':post})
Your urls.py seems to be setup ok, I think the reason it is not working for you is because you have extra spaces around the parameter. The correct way, in the templates, to pass arguments is like this:
{% url 'vehicle_movement:checkoutview' pk=vehicle_list_load.pk %}
I'm working on extending Horizon to include a custom app.
In that app, I have a DataTable:
class WorkloadsTable(tables.DataTable):
name = tables.Column("name", verbose_name=_("Name"))
description = tables.Column("description", verbose_name=_("Description"))
image = tables.Column("image", verbose_name=_("Image"))
flavor = tables.Column("flavor", verbose_name=_("Flavor"))
class Meta:
name = "workloads_table"
verbose_name = _("Workloads Table")
table_actions = (CreateNewWorkload, DeleteWorkload)
row_actions = (UpdateWorkload, DeleteWorkload)
which has a LinkAction:
class UpdateWorkload(tables.LinkAction):
name = "update"
verbose_name = _("Edit Workload")
url = "horizon:mydashboard:workloads_panel:update"
classes = ("ajax-modal",)
icon = "pencil"
def get_link_url(self, datum):
base_url = super(UpdateWorkload, self).get_link_url(datum)
workload_id = self.table.get_object_id(datum)
reversed = urlresolvers.reverse(self.url, args=[workload_id])
print reversed
return urlresolvers.reverse(self.url, args=[workload_id])
This LinkAction points to a routce in my urls.py:
WORKLOADS = r'(?P<workload_id>[^/]+)/%s$'
urlpatterns = patterns('',
url(r'^create/$', views.CreateView.as_view(), name='create'),
url(WORKLOADS % 'update', views.UpdateView.as_view(), name='update'),
url(r'^$', views.IndexView.as_view(), name='index'),
)
The issue is: When I enter the following url:
http://localhost:9000/mydashboard/workloads_panel/3/update
I get: NoReverseMatch: Reverse for 'update' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'mydashboard/workloads_panel/(?P<workload_id>[^/]+)/update$']
Where am I going wrong?
I guess the solution to your problem lies in the _update.html where you have to include the URL as,
{% block form_action %}
{% url 'horizon:mydashboard:workloads_panel:update' workload.id %}
{% endblock %}
while the workload.id is the object's id that you get in views.py in get_context_data function:
def get_context_data(self, **kwargs):
context = super(## the class name ##, self).get_context_data(**kwargs)
try:
context['workload'] = ##your API call ##(self.request,
self.kwargs['id'])
except Exception:
exceptions.handle(self.request)
return context
May be this will help you to resolve the error.
Well now the answer is quite simple. There is no matching URL for this reverse call:
{% url 'horizon:mydashboard:workloads_panel:update' %}
The update named URL pattern requires a valid workload_id argument matching your regex [^/]+
I am trying to make pretty meaningful urls, but I guess I'm doing it wrong.
This works:
from django.conf.urls.defaults import patterns, url
from places.views import explore_view
urlpatterns = patterns('',
url(r'', explore_view, name='explore'),
)
This does not:
from django.conf.urls.defaults import patterns, url
from places.views import explore_view
urlpatterns = patterns('',
url(r'(?P<countryorcategory>[0-9A-Za-z._%+-]+)', explore_view, name='explore'),
)
As I get this error:
Reverse for 'explore' with arguments '()' and keyword arguments '{}'
not found.
Here is the code for explore_view:
def explore_view(request, countryorcategory=None):
"""
This is the explore view - to view places sugeested by ambassadors
"""
user = request.user
page = request.GET.get("page", 1)
per_page = request.GET.get("per_page", 20)
category_id = request.GET.get("category_id", None)
attrs = request.GET
lat = safe_attr(attrs, "lat", "float", None)
lon = safe_attr(attrs, "lon", "float", None)
q = request.GET.get('q', None)
if q and not lat or lon:
cache_key = 'GoogleGeocode-{}'.format(hashlib.md5(q.encode('UTF-8', 'replace')).hexdigest())
latlon = cache.get(cache_key)
if not latlon:
latlon = geocode(q)
if latlon:
cache.set(cache_key, latlon)
if latlon:
lat = latlon['lat']
lon = latlon['lng']
if not q:
q = ''
category_names = getattr(settings, "EXPLORE_CATEGORIES", [])
categories = [Category.objects.get(name=cat_name).serialize() for cat_name in category_names]
more = True
places = Place.objects.explore_places(user, category_id=category_id, lat=lat, lon=lon, page=page, per_page=20)
if len(places) != per_page:
more = False
return render_to_response('explore/main.html', {'places': places, 'categories': categories, 'category_id': category_id, 'lat': lat, 'lon': lon, 'more': more, 'q': q}, RequestContext(request))
This line:
url(r'(?P<countryorcategory>[0-9A-Za-z._%+-]+)', explore_view, name='explore')
...is defining an url that takes an argument countryorcategory in the template. You need to put an argument on your url either of the following in your template:
{% url 'explore' argument %}
{% url 'explore' countryorcategory=argument %}
If you want to continue to use non-argument urls with the same name, you can define additional urls with the same name but with different patterns. For example:
urlpatterns = patterns('',
url(r'(?P<countryorcategory>[0-9A-Za-z._%+-]+)', explore_view, name='explore'),
url(r'', explore_view, name='explore'),
)
Then {% url 'explore' %} should work both with and without an argument.
For me, I forgot the namespace of the Route. Instead of
{% url 'login' %}
I should have written
{% url 'accounts:login' %}
with this configuration:
# root URLs
url(r'^accounts/', include('myproject.accounts.accounts.urls', namespace='accounts'))
# accounts URLs
url(r'^login$', views.login, name='login')
I'm assuming you are using a template with something like this :
{% url 'explore' argument %}
And this error probably means that the argument is not set to anything.
For whatever reason I'm having a tough time figuring out the correct variable to use in my url template tag to properly render my template.
#firms/url.py
from django.views.generic import list_detail
firm_list = {
'queryset' : Firm.objects.all(),
'template_name': 'firms/firm_index.html',
}
urlpatterns = patterns("",
(r"^$", list_detail.object_list, firm_list),
)
What should my {% url ??? %} variable be to render this firm_index.html template?
if you use the url style
urlpatterns = patterns('',
url(r'^index/$', index_view, name="main-view"),
...
)
you can give your url a custom name to use to refer to it
From the documentation, you can clearly mention the name of the view method and in the file.
{% url 'path.to.some_view' v1 v2 %}
So, for your case, you should be able to do:
{% url 'list_detail.object_list' 'firm_list' %}