سؤال

I am trying to setup a way that 2 people can work on one flask project without having conflict

In flask framework, the url response is like following:

in ~/application/urls.py

from flask import render_template
from application import app
from application import views

app.add_url_rule('/function1', view_func=views.function1)

in ~/application/views.py

def function1():
    #something here
return

in ~/application/__init__.py

import urls
#and other things

Now what I want to do is to have 2 coders, each person writes his/her own coder_urls. and coder_views in their own files

And then in the main urls.py and views.py I just import them from the files so the 2 coders can code separately and I can easily combine them.

I am guessing: coder1_urls.py

import coder1_views
app.add_url_rule('/coder1/function1', view_func=coder1_views.coder1function1) 
#this may need change

coder1_views.py

def coder1function1():
    #something here
    return

similar to coder2.

How do I do this? How should I arrange the files and write imports for my main urls/views files?

هل كانت مفيدة؟

المحلول

You can do it in several ways. I would suggest two of them.
First suggestion: Create a coders package containing the coders modules

~/application/__init__.py
~/application/coders/__init__.py
~/application/coders/foo.py
~/application/coders/bar.py
~/application/views.py
~/application/urls.py
...

You can import the coders functions in the views module:

from application.coders.foo import foo_coder_fucntion
from application.coders.bar import bar_coder_function

Second suggestion: Create a views package and dedicate a module view to each coder

~/application/__init__.py
~/application/views/foo.py
~/application/views/bar.py
~/application/views/main.py
~/application/urls.py
...

In the module you start the flask application:

import application.views.foo
import application.views.bar
import application.views.main

I prefer the first one.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top