所以我用这行在 root/project/urls.py 中创建了一个 url
from django.conf.urls import include
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
path('users/', include('app.urls'))
]
Run Code Online (Sandbox Code Playgroud)
在我的 root/app/urls.py 中
from django.urls import path
from .views import UserView, AuthenticationView
urlpatterns = [
path('register/', UserView.as_view()),
path('auth/', AuthenticationView.as_view()),
]
Run Code Online (Sandbox Code Playgroud)
所以预计会给我http://localhost:8000/users/register
和http://localhost:8000/users/auth
网址。
同时,我的请求没有按预期运行。
显然它在根路径和包含路径之间返回了一个空格。我检查了我的 root/project/settings.py 文件我没有发现任何奇怪的设置。有人知道发生了什么吗?
因此,我创建了一个自定义抽象类,该抽象类继承自UIViewController(由RebloodViewController继承)名为MainViewController的类。在这一节课中,我写了一个可重用的笔尖注册函数
class MainViewController: RebloodViewController {
typealias Cell = RebloodViewController.Constants.Cell
internal func registerNib(_ cellNib: Cell.Nib, target: UICollectionView) {
let nib = UINib(nibName: cellNib.rawValue, bundle: nil)
do {
let identifier = try getCellIdentifierByNib(cellNib)
target.register(nib, forCellWithReuseIdentifier: identifier)
} catch {
fatalError("Cell identifier not found from given nib")
}
}
private func getCellIdentifierByNib(_ nib: Cell.Nib) throws -> String {
var identifier: String? = nil
switch nib {
case .articles:
identifier = Cell.Identifier.articles.rawValue
case .events:
identifier = Cell.Identifier.events.rawValue
}
guard let CellIdentifier = identifier else …
Run Code Online (Sandbox Code Playgroud) 我试图用从 uuid4 生成的标识符创建一个模型。但是我想要的不是常规 uuid,而是标识符具有十六进制 uuid 格式(不带“-”)。这是我尝试过的:
class Model(models.Model):
identifier = models.CharField(max_length=32, primary_key=True, default=uuid.uuid4().hex, editable=False)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
def __str__(self):
return self.identifier
class Meta:
abstract = True
Run Code Online (Sandbox Code Playgroud)
不是每次继承类实例化时都返回唯一的 id,而是返回相同的 id,因为uuid4()
. 我试图将默认值从 to 更改uuid.uuid4().hex
,uuid.uuid4.hex
但似乎hex
不能uuid4
直接调用。那么从十六进制格式的 uuid 为我的标识符生成默认值的可能方法是什么?
我有一个ActivationTokenGenerator
创建将用于帐户验证的令牌,该令牌将通过电子邮件发送。例如,我使用时间戳、ID 和用户活动状态等参数对其进行了配置:
from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.utils import six
class ActivationTokenGenerator(PasswordResetTokenGenerator):
def _make_hash_value(self, user, timestamp):
return six.text_type(user.pk) + six.text_type(timestamp) + six.text_type(user.is_active)
account_activation_token = ActivationTokenGenerator()
Run Code Online (Sandbox Code Playgroud)
然后我使用account_activation_token
用于在我发送的验证电子邮件中生成令牌send_mail
。
@classmethod
def send_email(cls, request, user):
current_site = get_current_site(request)
mail_subject = 'Activate your Poros account.'
message = render_to_string('email_verification.html', {
'user': user,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)).decode(),
'token': account_activation_token.make_token(user),
})
to_email = user.email
email = EmailMessage(
mail_subject, message, to=[to_email]
)
email.send()
Run Code Online (Sandbox Code Playgroud)
一切看起来都很完美的电子邮件发送的令牌包含在一个 url 中,其模式如下:
url(r'activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
activate, name='activate'),
Run Code Online (Sandbox Code Playgroud)
在电子邮件中看起来像这样:
http://{{ domain }}{% …
Run Code Online (Sandbox Code Playgroud) 我正在尝试路由一条带有中间参数的路径。这是我的路线的样子。
import Events from "./views/EventsView";
import Event from "./views/EventView";
import NotFound from "./views/NotFoundView";
import RegistrationView from "./views/RegistrationView"
export default [
{path: "/", component: Events, exact: true},
{path: "/events", component: Events, exact: true},
{path: "/events/:projectId", component: Event, exact: true},
{path: "/events/:projectId/register", component: RegistrationView, exact: true},
{component: NotFound}
]
Run Code Online (Sandbox Code Playgroud)
然后我将我的路线映射到 a <Switch>
which 中 a 中<Router>
。
<Switch>
{
return routes.map(({path, component, exact}, key) =>
<Route key={key} path={path} component={component} exact={exact}/>)
}
</Switch>
Run Code Online (Sandbox Code Playgroud)
我Link
用来访问Event
组件。
<Link to="/events/1234">Title Here</Link>
Run Code Online (Sandbox Code Playgroud)
问题 …
javascript reactjs react-router react-router-v4 react-router-dom
django ×3
python ×3
hex ×1
ios ×1
javascript ×1
nib ×1
react-router ×1
reactjs ×1
swift ×1
token ×1
unit-testing ×1
url ×1
uuid ×1