项目的目录结构

myproject/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^console/', include('console.urls')),
url(r'^admin/', admin.site.urls),
]
console/models.py
from __future__ import unicode_literals
from django.db import models
class User(models.Model):
dev_key = models.CharField(max_length=40)
platform = models.CharField(max_length=50)
platform_id = models.CharField(max_length=24)
username = models.CharField(max_length=50)
channel = models.CharField(max_length=40)
def __unicode__(self):
return self.dev_key
class App(models.Model):
dev_key = models.ForeignKey(User)
app_id = models.CharField(max_length=24)
channel = models.CharField(max_length=150)
def __unicode__(self):
return self.app_id
console/admin.py
from django.contrib import admin
from models import *
admin.site.register(User, App)
console/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.welcome),
url(r'^create/', views.create_user),
url(r'^get/', views.get_dev_key),
url(r'^create_app/', views.create_app),
]
console/views.py
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponse
from django.db import connection
from console.models import User, App
def welcome(request):
return render_to_response('welcome.html')
def create_user(request):
if request.method == 'POST':
User(
dev_key=request.POST.get('dev_key'),
platform=request.POST.get('platform'),
platform_id=request.POST.get('platform_id'),
username=request.POST.get('username'),
channel=request.POST.get('channel')
).save()
return render_to_response('welcome.html', RequestContext(request))
def get_dev_key
(request):
if request.method == 'GET':
dev_key = User.objects.all().values(request.GET.__getattribute__('username'))
return render_to_response('welcome.html', {'dev_key': dev_key})
def create_app(request):
if request.method == 'POST':
dev_key = request.POST.get('dev_key_app')
cursor = connection.cursor()
cursor.execute("SELECT * FROM App WHERE dev_key = %s", dev_key)
row = cursor.fetchone()
if row:
App(
dev_key=dev_key,
app_id=request.POST.get('app_id'),
channel=request.POST.get('channel_app')
).save()
return render_to_response('welcome.html', {'dev_key_app': 'create success!'}, RequestContext(request))
else:
return render_to_response('welcome.html', {'dev_key_app': 'dev_key not exist!'}, RequestContext(request))
console/templates/welcome.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Welcome To JBox Console</title>
</head>
<body>
<form action="/console/create/" method="POST">{% csrf_token %}
dev_key:
<input type="text" name="dev_key"><br>
platform:
<input type="text" name="platform"><br>
platform_id:
<input type="text" name="platform_id"><br>
username:
<input type="text" name="username"><br>
channel:
<input type="text" name="channel"><br>
<input type=submit>
</form>
<br>
<form action="/console/get/" method="GET">
Type in username to search dev_key:
<textarea name="r_username" cols=60></textarea><br>
<p>{{ dev_key }}</p>
<input type="submit">
</form>
<hr>
<form action="/console/create_app/" method="POST">{% csrf_token %}
dev_key:
<input type="text" name="dev_key_app"><label >{{ dev_key_app }}</label><br>
app_id:
<input type="text" name="app_id"><br>
channel:
<input type="text" name="channel_app">
<input type="submit">
</form>
</body>
</html>
代码就这些了,setting的配置也加上了console应用,在admin.py中去掉App,可以运行,但是不明白为什么加上App不可以,我想在admin中也显示App表。本人django新手,求大神指路!