I have a Django-powered page running on Apache with mod_wsgi. It works just fine in Firefox. When I switch to Internet Explorer, however, none of my links work. They all drop the domain part of the link.
For example, in Firefox, if I mouse over one of my links, I see something like this:
http://mydomain.edu/pathtomystuff/linkpage/
and it works.
However, in Internet Explorer, the same link shows this when I mouse over it:
http:///pathtomystuff/linkpage/
and obviously doesn't work.
If I manually type the address in Internet Explorer, it works fine. It's just the links.
This is probably something obvious and boneheaded. Please forgive me :)
UPDATE
Well I did figure out something of a "solution". I had BASE href= {{request.path}} in my base html file (which all other pages in my site extend). In reviewing the source code shown by IE and Firefox, both were seeing BASE href= which means request.path was not being passed to my template. So I changed it to BASE href=mydomain.edu and it works in IE now.
This is not a great fix though because it takes away from the portability of the django app...
If you want to preserve portability (as well as maintainability), the easiest and most "djangonistic" way to proceed would be to use {% url %} tags in your templates.
Note that the {% url %} tag should be passed a view as 'argument', for example: {% url myApp.views.myView foo='bar' %} will use your urlconf to create the appropriate url that will point to the myApp view called myView, with the value 'bar' passed as the 'foo' arg.
Of course, you needn't have arguments in all your views, the foo='bar' part is purely optional (Please do note that you shouldn't specify the request argument)
The most common use would be to use a syntax such as:
MyLink
Basically what you're trying to do here is reinvent the url tag, it already exists, so use it! :)
Related
I am learning Django and i learn on making small project for lEARNING PURPOSE.
I learned how work with input and use as variable. Now i want to use search box with the same principle.
Please see the link of input based :
https://stackoverflow.com/questions/58813945/changing-form-to-search-box
index.html is the input after inserting info and clicking ok it directs to index2.html which shows the results
What I need to change besides POST to GET to make it work as it works with input function? do i need to change form, models?
My concern also i want to inset search bar in the navigation in index2.html where user can make search from the result page. This is my index2.html page. I tried to put {{form.as_p }} inside the form function but it does not work. I want after putting infro in search box and clicking search glyphicon (which does not work) will stay on the same page index2.html
I searched the internet and read stackoverflow other examples, but they use complicated version, i need a simple one :
1. i want to have search box on index.html page so it does not show me the name in front of input box and
2. i want search box in the index2.html page keep implementing the same function as index.html so a user do not have to go the initial page to search new data.
Any help or tips would be highly appreciated.
Since you're using bootstrap4 I would recommend to use its template tags and form field customization options.
Example:
{% bootstrap_field form.login show_label=False addon_before='<span class="fa fa-user"></span>' %}
Not: this example uses Font Aweseome for icons.
Also you're not using the real power of Django Bootstrap4, see Quickstart. You can always override the version of Booststrap or the CDN used in your settings.py if needed: Settings.
My site using URL Rewrite to make SEO friendly URLs. This makes self-posting a form back to the same page a little tricky.
However in ColdFusion I do this for the form's action attribute:
<form name="formSortBy" method="post" enctype="multipart/form-data" action="#StructFind(GetHttpRequestData().headers, 'X-Original-URL')#">
</form>
The important part here is the #StructFind(GetHttpRequestData().headers, 'X-Original-URL')# which gets me the URL of the page.
However the X-Original-URL key just doesn't exist on some pages so I get an error from ColdFusion saying:
Cannot find X-Original-URL key in structure.
The specified key, X-Original-URL, does not exist in the structure.
This is happening when I click to go to the homepage of a section I am in.
So X-Original-URL exists if I go to http://www.sitename.com/products/gaming but it won't exist if I go just to http://www.sitename.com/products
Is there anyway to get around this or make it work like I need it to?
Sounds like there is an issue between your rewrite rules and CF.
But, there is an easy fix - you can get a form to post to itself by simply not specifying the action attribute.
Django 1.5.1
Experience, getting started
I'm currently working on my static side of the site.
And creating the HTML/CSS/JS stuff. In the base html i have some links.
One of them is "about" that will lead to the ...:8000/about
Now when im on the about page there are the same links cause they are in the base template.
When i click on them now i get ...:8000/about/about
and it will go on adding the /about each time i click.
How should i get this link to always point to ...:8000/about
Thank you.
Absolute URLs.. start your links with /
<a href="/about/"> instead of <a href="about/">
Also, if it will "add /about" each time you click, that means you're re-rendering your view... which means your URLConf probably has a too-broad regex (make sure your line is terminated by a /$
Kind of sounds like you have a line like url(r'^about/', 'foo') where /about/about/about will continue to match.
It's also best practice to use the {% url %} tag via named urls. Sooner or later you'll change a URL and thank everyone for it.
https://docs.djangoproject.com/en/dev/topics/http/urls/#named-groups
I'm working on a Django project that is getting bigger and bigger. I'm now reorganizing the views.py file, separating it into different modules in the views package I've created.
After moving the view functions, I had to find all references to them. Changing urls.py was easy. Finding errors in reverse calls was also easy - Eclipse marked the ones that had a problem.
However, I can't find a way to go over all {% url %} template tags and find the ones that don't reference a view. I can go over them one by one, but any typo will lead to a runtime error.
Is there an automatic way to check {% url %} tags for invalid view names?
Yes there are couple of ways to do it.
Most of IDEs have something for rename refactoring, PyCharm have this feature to find the usage of the object, string, etc... in your current project, Then gives you a report about it.
Anyway, Since you renamed it already you can write some tests for it. Tests are prefect for your case.
What about creating test cases for your views that at the very least make sure they return a 200 response? Although this would require more front-end work and obviously wouldn't accomplish exactly what you're looking to do, it would help you identify the views that had these named url errors and allow you to programmatically test your entire site in the future.
As you can read on the django reference or hacking a bit, get_flatpages can be used as follow:
{% get_flatpages as flatpages %}
{% get_flatpages for someuser as flatpages %}
{% get_flatpages '/about/' as about_pages %}
{% get_flatpages prefix as about_pages %}
{% get_flatpages '/about/' for someuser as about_pages %}
So, if I want to get a specific page I need to do it via its url or a prefix, which is somewhat rough, because my template code become data dependant, I meant, if I change the url of certain flat page then it is necessary to change my template code too.
A more flexible idea would be including an identifier to each page, addable through e.g. the 'Advanced options' section, so that the page can be refered via its identifier, thus we could do something like this:
{% get_flatpages 'about' as about_pages %}
Which is more flexible and less data dependant, no matter what url the page has, note we could change the page's url without changing the template code.
Is there something like that in the framework?, of course I could customize this app or to use a third-party app, but this isn't the point ;-)
Have you any other idea to deal with?
No, I don't believe there's any support for what you're asking for in Django at present. The docs you linked to for flatpages say:
A flatpage is a simple object with a URL, title and content. Use it for one-off, special-case pages, such as “About” or “Privacy Policy” pages, that you want to store in a database but for which you don’t want to develop a custom Django application.
My reading of this is "this is the bare minimum we're providing, and if you want any more you'll have to code it yourself". I agree that your proposal of allowing pages to be referred to by a symbolic name (which perhaps defaults to their URL) is more flexible, but you're probably better off raising an issue for it or discussing on the mailing list than hoping a dev happens upon your question on StackOverflow.
In the short term, you could look at some of the pre-built CMS-like Django apps. Django-page-cms is relatively lightweight (it's a relatively small app itself but does have a bunch of dependencies) and you could accomplish what you're after: each page is referred to by its slug (aka url / name), but you can define aliases / redirects to given pages for arbitrary URLs.