Cannot Get Haystack Tutorial Working - python

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 }}

Related

Django URL and Template with inbuilt authentication

I am a Django Beginner, I started by reading WS Vincent..The book created a customUser model in a separate App name USERS. Also, AUTH_USER_MODEL = 'users.CustomUser' has been set up. I have below question related to URLS and Templates . Any help will be appreciated
I have been reading that the Default Django login path will go to /accounts/login. However , when I used {% url login %} in template base.html it routed to users/login. That would be coz fo Auth_user_model, but I want to be sure how the above tag would fit in below URL's because there is still no accounts/login URL. If it is getting that from auth.urls package then it only has everything starting with /accounts not /user. I did a packet capture thinking it might be translating to account/login but destination was still users/login. .I hope I was able to explain my query. Please help.
urlpatterns = [
path('admin/', admin.site.urls),
path('users/', include('users.urls')), # new
path('users/', include('django.contrib.auth.urls')),# new
path('', TemplateView.as_view(template_name='home.html'),name='home'), # new
]
(if i understand your question correctly) there is a default value in settings.py which you have to override
https://docs.djangoproject.com/en/3.1/ref/settings/#std:setting-LOGIN_URL

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 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)),

Passing slug from Template to View in django

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'),

Django image not getting uploaded

We are making our school site on Django, we made a gallery app but i think the images are not getting uploaded to designated folder, rather they are not even getting uploaded i think so .
Code : -
# views.py
def upload(request, template_name="gallery/form.html"):
form = GalleryForm(request.POST or None, request.FILES or None)
if form.is_valid():
form.save()
messages.success(request, "image has been uploaded")
return redirect('gallery.views.index')
return render(request, template_name,{'form':form})
from django.db import models
# models.py
class GalleryPhoto(models.Model):
image = models.ImageField(upload_to='pic_folder/')
description = models.CharField(max_length=50)
def __unicode__(self):
return self.image.url
The rest of the code can be seen in this repo
Note : the the gallery template contains some conflicts but that is not the actual problem.
It took me a while to notice it, because it's kinda tricky. But in retrospect it's obvious.
So after configuring MEDIA_URL = "/MEDIA/" I noticed that http://127.0.0.1:8000/media/ was giving me a 404. If everything is configured correctly (and it is) then usually the problem is conflicting urls. See the url patterns used in django are always evaluated in order. That means if you have urls like so:
url(r'^mypage/', 'myapp.mypage'),
url(r'^mypage/another/', 'myapp.different')
Then the second url will never be available. Why? because there's nothing up there that tells the url dispatcher that the first url should end at mypage/. As far as it's concerned, everything starting with http://DOMAIN/mypage/ will be caught and passed to the first function.
To avoid that, we use the $ sign to tell the dispatcher at what point to stop. Like so:
url(r'^mypage/$', 'myapp.mypage'),
url(r'^mypage/another/$', 'myapp.different')
Now let's look at your last two urls:
url(r'^', include('pages.urls')),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
See that? url(r'^' swallows everything that comes after it, so no media is being served. It's true that pages.urls uses a dollar for its first url:
url(r'^$', views.index, name='index'),
but that doesn't matter for the main url dispatcher, which evaluates r'^' first. What's the solution? the simplest would be to move the pages urls to the end:
url(r'^ckeditor/', include('ckeditor.urls')),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += patterns('', url(r'^', include('pages.urls')))
You can also isolate that one problematic url from the others using a direct call, like so:
url(r'^/$', 'pages.views.index'),
url(r'^pages/$', include('pages.urls')),
After that, this will work:
<img src="{{ MEDIA_URL }}{{ image.image.url }}" alt="{{ image.description }}">
p.s.
Eventually you're going to pass the media and static files through your server, when you move to deployment level, so maybe the whole thing wouldn't matter then. If you do decide to keep the r'^' in use, just keep in mind that it might cause conflicting problems with other urls, so be sure to always put it last.

Categories

Resources