开发者

How to shift thinking from ASP.NET Web Forms to Django?

I recently moved from working with ASP.NET Web Forms to working with Django. From my point of view, they seem like two different worlds entirely. While ASP.NET is very top-down, encouraging and thoroughly supporting working from HTML/CSS visuals and building the backend along the way, Django seems much more bottom up, always starting with the database.

The fact is, I often have a hard time reconciling the two. Any tips 开发者_如何学编程as to how to make all of this jive in my brain?


You should still continue to think interface -> database. That is the more correct way to think about projects as a whole.

But when actually writing code - yes you're going to have to start with your models. Luckily, changing fields around is really easy (especially when you use south for migrations). And fields pass straight through to forms (if you don't need to modify anything). And you can display forms without referencing every single field (e.g. {{ form.as_ul }} ). So django is very transparent in that regard, layers "shine through" onto other layers of the framework.

As for metaphors - urls are all treated through regular expressions. It merely points a url towards a function to run with the appropriate arguments. So you're not really writing "pages" as you are in .NET - you're just writing a function to handle the args, gather the data, and spit a template out. The template is the closest thing to a "page" in django. As each individual page (or part of a page) can extend another page (usually it is only one level, which is the outer template... stuff like ...)

The majority of your code belongs in the models. Then forms. Then view functions. And finally templates. Templates should be small. Re-use is your friend. These are just some of the design principles they were going after when they created django. Hope this helps :)


In my opinion you don't have to start with the database in django.

I usually start with some vary basic html, then css to get the primary design elemenents right.

When I have a sense of how I want the site to look and feel, I start "templating" my html files. filling in variables, filters and tags accordingly.

When all this is done I usually have some hardcode html "dummy" models… like users: first name, last name, age, city etc.

When this is done I start on my models. This is much easier when you what you want to show in your templates… and then just name the model fields accordingly.

But having never used ASP.Net, I'm not sure if this is of any help :)

guess you can use django in a lot of different ways.. which again is why we all love it :-D

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜