Example: Car Website.
If you have a URL structure that goes like this /maker/model/year. Where maker, model, year are the generic default placeholder for any. And replace any part will filter the results.
So:
/maker/ will list give you a list of car makers like VW or Ford.
/Ford/model/ will list all models made by Ford
/maker/model_of_car/ will list all models with that name (if two makers have the same name of the model it will list both.
/maker/model/ will list all models
etc... if you need more example please say. But I hope this is enough to get the idea across.
What is the optimal number of URLs need to cover all possibility? Can you show if not all, a critical number of example to get the idea across. So in the form url(r'(?P<maker>[w-]+)/(?P<model>[w-]+/)' (optimal meaning: NOT making just making one super URL that will require a complex view and template to make it work.)
I am sorry about the question name, can someone change it to fit the body (if required). I feel like it does not do the body justice.
Thank you for your time.
I believe this will work for your needs, but I think you should rethink your url convention. Look at how Django Rest Framework builds it's ViewSet's urls. it will work great for you
url(r'^maker/$', <view>),
url(r'^maker/model/$', <view>),
url(r'^maker/(?P<model_of_car>[\w ]+)/$', <view>),
url(r'^(?P<maker>[\w ]+)/model/$', <view>),
Related
So I want to make it so a "Tutor" can select a "Student", now each tutor has a different set of Students so the drop down list needs to be unique.
So this is the part of forms where question comes from, tutor1 get's passed through by response into views, then from views it needs to be passed into the form so Students.objects.all displays all of the students that tutor1 has. (The query set part is fine and I believe the Field is also correct) the part that doesn't work is passing in response or passing in the tutor1 into the form and so far I can't find anywhere how to do this.
student = forms.ModelChoiceField(queryset = Students.objects.all(tutor=tutor1.id))
This is the last piece of the hard part of my website so I'd be so grateful if anyone knows how to fix this.
Nevermind think I found an answer, you can't haha. But the strategy instead is using class based views.
https://medium.com/analytics-vidhya/django-how-to-pass-the-user-object-into-form-classes-ee322f02948c
This page explains it really well.
For example, there are two applications cars and houses, both of them currently have a models.ManyToManyField to a model with a models.ImageField(upload_to='img/[APP_NAME].
Instead of copying and pasting that snippet of ImageField code, is it better to split the image upload into a a separate application and import it? What should it look like?
There's no clear answer on your question. It highly depends on what you want to achieve.
With making a separate app, that only knows about images, you will have the following problems:
You want different upload_to for your cars and houses. You will need to set it dynamically. Example.
Your image model (the one with ImageField) will know about both cars and houses, via related_name property. I don't find it to be any good. To get rid of the backreferences, you can specify ManyToMany(..., related_name="+"). Note: "+" means no related name given and it shouldn't be auto-generated. But the reasonability of removing it is also questionable.
One more app, that does... well, nothing. One more dependency for each of your apps.
On your place I would just leave both model images, one in cars, another in houses. That's considering the amount of boilerplate code you will have to write and amount of problems you would create for yourself out of nowhere.
Apologies if this has been discussed before, have searched and searched but didn't find anything useful :)
But here goes.
We're currently in the process of rewriting a portion of our webapp. Our app is rather old and therefore suffers from some rather cowboy'ish approaches to programming, conventions and urls.
What we're looking for is a simple clean way to design our views and urls so that we can maintain both easier in the future.
The problem is; as of now our urls.py file for the main site is one big mess. a lot of urls that point to a unique view that only does one thin.
Ex. list_books/, edit_book/ etc.
when it comes to specific formats etc. we have something like list_books_json/
(these aren't the actual urls though, but just used to prove a point since the real urls are much worse)
What we want to do now is clean it up a bit. And we we're wondering what the best way to get around it would be??
What we have thought of so far(after reading a lot of things on the subject):
We've thought of designing our urls after the following pattern:
domain/object/action/
so the urls for the apps "staff" site for changing books in the app would be:
staff/books - to view all books (GET)
staff/books/ID - to view one books (GET)
staff/books/new - to create a new book (POST)
staff/books/ID/edit - to edit specific books (POST)
staff/books/ID/delete - to delete specific books (POST)
The thought was then to have only 1 view, views.staff_books() to handle all these actions when dealing with books through the "staff" part of the site.
so that staff_books() checks for ID or a certain "action" (edit, new, delete etc.)
The result would be fewer, but a lot larger views that have to handle all aspects of staff/books. Right now we have a ton of small views that handle only one thing.
Does this makes sense, can you see potential problems? How do you guys go about it??
One place where I think we're lost is in regards to formats.
Where would you put ex. the request for returning the response in json?
we're wondering "staff/books.json" or "staff/books/ID.json" etc. and then keeping all the json logic in the same "staff_books()" view.
So thats it basically. I'm sorry the question is a little "fluffy"... We basically need some examples or good design advice as to how to structure urls and views.
Kind Regards
pete
As an extension (and solution) to your problem I would suggest to use the strategy pattern. Since you already have a structure and the only thing that differs is "how" it is supposed to be carried out, this pattern fits your problem perfectly. What I mean by that is the following:
Create a view which is your entry point to your application with functions named as your url-based functionality (edit, new, delete etc.). I.e where your url.py determines where to go from there.
Create classes which do your stuff based on your domains etc. Lets call them Book, Calendar etc for now.
Implement functionality of those classes, like edit, new, delete etc.
in your view then, determine what class to instantiate and call the corresponding function, e.g in View.edit() call domain.edit()
I think that should do it ^^
Hope it helps :D
Im actually working in a django project and I'm not sure about the best format of the URL to access into one particular object page.
I was thinking about these alternatives:
1) Using the autoincremental ID => .com/object/15
This is the simplest and well known way of do that. "id_object" is the autoincremental ID generated by the database engine while saving the object. The problem I find in this way is that the URLs are simple iterable. So we can make an simple script and visit all the pages by incrementing the ID in the URL. Maybe a security problem.
2) Using a <hash_id> => .com/object/c30204225d8311e185c3002219f52617
The "hash_id" should be some alphanumeric string value, generated for example with uuid functions. Its a good idea because it is not iterable. But generate "random" uniques IDs may cause some problems.
3) Using a Slug => .com/object/some-slug-generated-with-the-object
Django comes with a "slug" field for models, and it can be used to identify an object in the URL. The problem I find in this case is that the slug may change in the time, generating broken URLs. If some search engine like Google had indexed this broken URL, users may be guided to "not found" pages and our page rank can decrease. Freezing the Slug can be a solution. I mean, save the slug only on "Add" action, and not in the "Update" one. But the slug can now represent something old or incorrect.
All the options have advantages and disadvantages. May be using some combination of them can some the problems.
What do you think about that?
I think the best option is this:
.com/object/AUTOINCREMENT_ID/SLUG_FIELD
Why?
First reason: the AUTOINCREMENT_ID is simple for the users to identify an object. For example, in an ecommerce site, If the user want to visit several times the page (becouse he's not sure of buying the product) he will recognize the URL.
Second reason: The slug field will prevent the problem of someone iterating over the webpage and will make the URL more clear to people.
This .com/object/10/ford-munstang-2010 is clearer than .com/object/c30204225d8311e185c3002219f52617
IDs are not strictly "iterable". Things get deleted, added back, etc. Over time, there's very rarely a straight linear progression of IDs from 1-1000. From a security perspective, it doesn't really matter. If views need to be protected for some reason, you use logins and only show what each user is allowed to see to each user.
There's upsides and downsides with every approach, but I find slugs to be the best option overall. They're descriptive, they help users know where there at and at a glance enable them to tell where they're going when they click a URL. And, the downsides (404s if slugs change) can be mitigated by 1) don't change slugs, ever 2) set up proper redirects when a slug does need to change for some reason. Django even has a redirects framework baked-in to make that even easier.
The idea of combine an id and a slug is just crazy from where I'm sitting. You still rely on either the id or the slug part of the URL, so it's inherently no different that using one or the other exclusively. Or, you rely on both and compound your problems and introduce additional points of failure. Using both simply provides no meaningful benefit and seems like nothing more than a great way to introduce headaches.
Nobody talked about the UUID field (django model field reference page) which can be a good implementation of the "hash id". I think you can have an url like:
.com/object/UUID/slug
It prevents from showing an order in the URL if this order is not relevant.
Other alternatives could be:
.com/object/yyyy-mm-dd/ID/slug
.com/object/kind/ID/slug
depending of the relevant information you want to have in the url
I'd like to list all active products (from all or a specific category) in a template. I've looked almost everywhere and I simply cannot find a way to do this.
I want to display them in the footer of the shop (10 products from 1 category). That means show them without selecting product category.
Is this even possible? Products are only listed in the category template...
I'm using Satchmo 0.9.2
EDIT: Somehow I've missed this:
http://www.satchmoproject.com/docs/dev/customization.html
So it's solved...
Thank you!
this is a more general answer since there isnt any answer yet, so dont beat me. You also have to know that I never used satchmo, I never had a look at it.
But despite this, if I had to deal with your situation, I would have a look at the source code.
You might find answers there to develop something custom for your situation. This can be a tricky task but at least its worth a try.
There have to be models which store the data for your product and categories. Have a look at them and the views that retrieve the products from the database to render them. Also a look into the database cant hurt (think of phpmyAdmin to have a nice webbased interface).
It can be helpful to fire up your ./manage.py shell, import your/satchmos product and category models and play around with them.
A possible solution then could be to write a custom context_processor which retrieves the needed products/categories and passes these products from a category to your footer on a more global basis. Have a look here https://docs.djangoproject.com/en/1.3/ref/templates/api/#writing-your-own-context-processors. Maybe a custom middleware could also be a possibility. https://docs.djangoproject.com/en/dev/topics/http/middleware/#writing-your-own-middleware
I hope this helps. At least worth a try :)