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
Related
Ive changed certain parts of my admin page and played around extending the templates (so I have the file structure set up and working). I now want to go a bit further.
I want to add a column next to the 'recent activity' column on the admin page which would list all the most recent django-notifications the admin has received.
I am guessing I'd extend the base.html and add my notifications there. My first question is is this the best place to do this?
The next, bigger thing is that I would need to modify the original views.py file to do this. Surely this isnt a good idea? Poking around in the Django Admin views.py sounds like a terrible idea and makes me think that this kind of thing shouldnt be done, however id still like to know for sure before I give up on it.
I can find information on adding views to Django Admin, but nothing on adding content like this to the admin front page.
Thank you.
This won't be overly difficult to do.
A rough outline, you'll need to use your own AdminSite, and overwrite the index method (this is the method which renders the main view). Something like this:
from django.contrib import admin
class MyAdminSite(admin.AdminSite):
index_template = "path/to/my_template.html"
def index(self, request, extra_context=None):
# do whatever extra logic you need to do here
extra_context = ... # this will be added to the template context
return super().index(request, extra_context=extra_context)
Notice I've also added in a new index_template. This is what the index method will use to render it's response. So we had better set that up. We'll extend the old index.html and just add in our extra code in the bit that we want.
{% extends "admin/index.html" %}
{% block sidebar %}
{{ block.super }} // this just includes everything that was there before
// add your extra html here
{% endblock %}
And this should do the trick :) So, no. You don't really have to go messing around with anything you shouldn't be.
You'll want to register the MyAdminSite class as your new admin. There are clear instructions to do that in the django docs.
However... It is always worth thinking about the first few lines from the django-admin docs:
The admin’s recommended use is limited to an organization’s internal management tool. It’s not intended for building your entire front end around.
The admin has many hooks for customization, but beware of trying to use those hooks exclusively. If you need to provide a more process-centric interface that abstracts away the implementation details of database tables and fields, then it’s probably time to write your own views.
I would go a step further and say it should probably only be used by fellow developers. It's far to easy for others to cause major problems. So depending upon the reason why you want to add in these notifications here, it may or may not be a good thing to do.
I hope this helps :)
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.
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.
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! :)