Passing slug from Template to View in django - python

I have an application wherin I am trying to pass a slug field from a template (frames.html) to the views.py. My problem is that I see the URL changing on clicking on the link on frames.html, but it doesn't seem to reach the view.(I have print statements in my views.py which are not getting printed, so that's how I know.). My frames.html is as follows
<div id="FramePage">
{% for frame in Frames %}
<p>{{frame}}
{% endfor %}
</div>
This is the entry in my urls.py
urlpatterns = patterns('',
# Examples:
#url(r'^$', 'vTryON_Django.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'vTryON.views.home_page'),
url(r'^frames/', 'vTryON.views.VTryONAll'),
url(r'^frames/(?P<frameslug>.*)/$', 'vTryON.views.selectedFrame'),
url(r'^tryonpage/', 'vTryON.views.tryonpage'),
url(r'^uploadPC/', 'vTryON.views.uploadPC'),
url(r'^uploadWebcam/', 'vTryON.views.uploadWebcam'),
)
and this is my view function in views.py
def selectedFrame(request, frameslug):
#print('selectedFrame')
frame= VTryON.objects.get(slug=frameslug)
#print(frame)
context={'frame':frame}
#print('context')
return render_to_response('selectedframe.html', context, context_instance=RequestContext(request))
As I mentioned, i can see my URL change from http://127.0.0.1:8000/frames/ to http://127.0.0.1:8000/frames/FR123/ after I click on the link on frames.html.
Am I configuring it wrongly in urls.py? I am a beginner in python /django. Please help me out.Thanks in advance.

The problem is in your URLs. Your frames URL does not terminate the regex at the end. So it also matches anything that simply starts with "frames", which includes "frames/FR123".
You should ensure that all URLs explicitly end with $ to prevent this.
url(r'^frames/$', 'vTryON.views.VTryONAll'),

Related

Unable to resolve URL string for path using name with reverse

I have been spending the better part of my week learning Django to build my website and I've stumbled upon an issue I can't seem to get working. I wish to resolve a URL string for a path by including with that path a particular name and then being able to reference that name down the line. This works fine until I change the route in path to use the include method.
First Attempt:
from django.url import include, path
from . import views
urlpatterns = [
path('testapp/', include('testapp.urls'), name='testapp'),
path('about/', views.about, name='about'),
]
Now when I call {% url 'about' %} from my template html file I get back '/about/' as expected but when I try and call {% url 'testapp' %} I get a NoReverseMatch exception instead of getting '/testapp/'. After digging through the documentation I stumbled upon this example that shows path with include using a namespace instead so I adapted the above a bit.
Second Attempt:
# from mysite/urls.py (adapted from before)
from django.url import include, path
from . import views
urlpatterns = [
path('testapp/', include('testapp.urls', namespace='testapp')),
path('about/', views.about, name='about'),
]
#from testapp/urls.py
from django.url import include, path
from . import views
app_name = 'testapp_name'
urlpatterns = [
path('', views.index, name='testapp_index'),
path('directory/', views.directory, name='testapp_directory'),
]
Now from the previous example I try using the namespace in lieu of a name {% url 'testapp' %} and I again get the same NoReverseMatch exception however using the namespace and name from my included url {% url 'testapp:directory' %} does work giving me '/testapp/directory/'.
I know there's some concept I'm not getting or something I'm overlooking but I'm just running around in circles at this point and would really appreciate any help somebody could afford me.
If it helps I'm trying to get the path so that I can use it in a navigation bar to highlight the currently activated tab. I'm also not hardcoding the paths as I'm trying to keep it DRY although at this point if I can't get it done I might have to but I'm hoping someone has a much better idea of what they're doing and could point me in a helpful direction. I appreciate all assistance and thank you!
The problem is that testapp is not a single view: it is an include(..), so it encapsulates a collection of views:
from django.url import include, path
from . import views
urlpatterns = [
path('testapp/', include('testapp.urls'), name='testapp'),
path('about/', views.about, name='about'),
]
It is not said that this collection contains a view at all, or it can contain multiple. Even if it only contains a single view, then it would be unsafe since you can later change your mind, and add an extra view.
If there are two or more views, then how will you decide what view (and therefore URL) to take? If the include(..) has two views: the "homepage" and the profile page, then this makes a significant difference.
You thus should refer to a real name, and whether you give the include(..) a namespace in the include(..) is irrelevant:
{% url 'testapp_name:testapp_index' %} <!-- first attempt -->
{% url 'testapp:testapp_index' %} <!-- second attempt -->
To reference the name of a real view.
{% url 'testapp_name:testapp_index' %}
or
{% url 'testapp_name:testapp_directory' %}
as you are using app_name in the urls file, you need to mention it with the name of the view

Django redirect based on parts of url pattern

So I have this small simple problem that I can't really find a solution to.
I would like to redirect all links on the form r'^blogg/ to r'^blog/'. I would think there is a solution similar to the accepted answer in this post here, but it doesn't work that well. Note that the blog-application has many sub-url-patterns, so a solution like RedirectView.as_view(url="/blog/") would not work.
In my main urls.py
urlpatterns = patterns('',
url(r'^blogg/', RedirectView.as_view(pattern_name="blog")),
url(r'^blog/', include("blog.urls", namespace="blog")),
The solution above returns HTTP 410 (Gone) for all sub-urls of blogg. I suspect this is due to the missing url argument in RedirectView.as_view().
Thanks in advance for all answers!
You won't be able to do this entirely in urls.py. But a simple view function could work:
url(r'^blogg/(?P<rest>.+)$', views.redirect_prefix),
from django.shortcuts import redirect
def redirect_prefix(request, rest):
return redirect('/blog/{}'.format(rest))
What about
urlpatterns = patterns('',
url(r'^blogg/',include("blog.urls", namespace="ignoreme") ),
url(r'^blog/', include("blog.urls", namespace="blog")),
it both should strip the first part (blog/ or blogg/) from url and then manage the rest equally. And if you will use {% url "blog:this_view" %} then on next page your customer will be on the blog/ part anyway

How to djangocms-link to a app-hook subpage?

I have an app that provides a form to create and update "Prediction"s.
urls.py:
urlpatterns = patterns('',
url(r'^create/$', PredictionCreateView.as_view(), name='create'),
url(r'^(?P<pk>\d+)/$', PredictionDetailView.as_view(), name='detail'),
url(r'^$', PredictionListView.as_view(), name='list'),
)
The app is attached with an app-hook to Django CMS 3.0.12.
cms_app.py:
class Predictionhook(CMSApp):
name = "Predictionhook"
urls = ["prediction.urls"]
app_name = "prediction"
apphook_pool.register(Predictionhook)
Is there an easy way to include the 'create' URL of the attached app in the list of possible choices of pages in the djangocms-link plugin?
The standard setup won't do it as far as I'm aware.
I'd create your own copy of djangocms-link and extend it to give a field to take the same thing you'd put in a template URL tag. Then just set up the template to render that field value as a standard Django URL template tag.
So in your new field of the link app you'd add prediction:create and then in the template being rendered you'd have {% url link.myurllinkfield %}

Django admin site with url parameters

I have a Django project with several applications, and I want to add the Django admin site for one of these.
The problem I have is that the main urls.py file has
url(r'^tools/(\w+)/', include('tools.myapp.urls')),
and in my myapp.urls I have added
url(r'^admin/', include(admin.site.urls)),
The problem is that the parent part of the url matching uses a parameter, which is usually passed in the template (that's the application name) like so
{% url "my_view_function" request.info.appname %}
But the default Django template obviously don't include that extra parameter, when calling
{% url 'admin:logout' %}
thus leading to a NoReverseMatch exception.
How can I have the admin site working?
If you just want to have admin run under any tools/whatever/admin URL, you can add this line before the tools.myapp.urls import in your main urls.py:
url(r'^tools/\w+/admin/', include(admin.site.urls)),

Cannot Get Haystack Tutorial Working

I have completed the tutorial for Haystack w/ Whoosh and re-read it about 6 times in the past few hours and cannot seem to figure out what my issue is. Basically, everything seemed to installed correctly, however when I visit the /search URL all I see is my base template without the search template (no input box/header/etc). This is my urls.py at the application level:
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^search/', include('haystack.urls')),
url(r'a/list/$', views.admin_list, name='admin_list'),
url(r'a/list/(?P<list_id>\d+)/edit/$', views.admin_edit, name='admin_edit'),
)
The template lives in myapp/templates/search/search.html. I have added nothing to my views.py, because the tutorial didn't go over adding anything to that file. What am I missing here?
What is content of the data template file ( appname_text) file , if you are keeping document =true in search_index.py file your data template should have the fields that are to be searched .
data template for will be something like.
{{ object.foo }}
{{ object.bar }}

Categories

Resources